xamarin / GooglePlayServicesComponents

Other
314 stars 146 forks source link

Google Cast failed on Android 12 - Missing Flag_Immutable #576

Open valentin-debris opened 2 years ago

valentin-debris commented 2 years ago

Xamarin.Android Version

Minimum 5.0, Target 12.0

Operating System & Version

Windows 10 - Tested on real device Pixel 6 (Android 12.0)

Google Play Services Version

Describe your Issue

With Android 12, the Cast crashes the app as soon as I select the device to Cast. Everything works well lower Android 12. I noticed that if I comment this line, it doesn't fail, but I can't do anything without that line !

Minimal Repro Code Sample

You can test with this example https://github.com/valentin-debris/XamarinTestCast

Steps to Reproduce (with link to sample solution if possible):

  1. Run the app on Android 12
  2. Click on the Cast button
  3. Select the device
  4. Cry because it crash !

Include any relevant Exception Stack traces, build logs, adb logs:

FATAL EXCEPTION: main
Process: com.companyname.testcast, PID: 25895
java.lang.IllegalArgumentException: com.companyname.testcast: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
 at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
 at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
 at com.google.an02-17 11:51:01.761 E/AndroidRuntime(25895):   at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
 at com.google.android.gms.internal.cast.zzch.zzb(com.google.android.gms:play-services-cast@@20.0.0:1)
 at com.google.android.gms.cast.framework.media.internal.zzp.zzb(com.google.android.gms:play-services-cast-framework@@20.0.0:10)
 at com.google.android.gms.cast.framework.CastSession.zzi(com.google.android.gms:play-services-cast-framework@@20.0.0:11)
 at com.google.android.gms.cast.framework.zzk.onComplete(com.google.android.gms:play-services-cast-framework@@20.0.0:1)
 at android.os.Handler.handleCallback(Handler.java:938)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loopOnce(Looper.java:201)
 at android.os.Looper.loop(Looper.java:288)
 at android.app.ActivityThread.main(ActivityThread.java:7839)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
jpd21122012 commented 2 years ago

I have the exact same issue

moljac commented 2 years ago

@valentin-debris @jpd21122012 Thanks for the feedback.

@valentin-debris And thanks for the repro sample. Helps a lot.

valentin-debris commented 2 years ago

Hello @moljac , any news about this issue ? Until a fix, we have to disable this functionnality in our app for OS12, which is not good for our customers that upgrade and miss it now :/

moljac commented 2 years ago

Hello @moljac , any news about this issue ?

Sorry, but no. Nothing yet. Trying to catch up with updates - a lot of new dependencies.

Until a fix, we have to disable this functionnality in our app for OS12, which is not good for our customers that upgrade and miss it now :/

OK from here (quick search):

https://stackoverflow.com/questions/68473542/mediasessioncompattargeting-s-version-31-and-above-requires-that-one-of-flag

Workarounds to try

  1. add nuget Xamarin.AndroidX.Work.Runtime 2.7.1 (2.7.1.2)
  2. or add Xamarin.AndroidX.Media 1.4.1+ https://www.nuget.org/packages/Xamarin.AndroidX.Media/1.4.1
  3. or try adding this snippet, but ported to c#
    PendingIntent pendingIntent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity
               (this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
    }
    else
    {
         pendingIntent = PendingIntent.getActivity
                (this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    }
valentin-debris commented 2 years ago

@moljac , with my repo for testing, I tryed the first 2 solutions and they failed, same error. And the third one, I can't use it since the place where PendingIntent.getActivity() is not in my code, but somewhere in GooglePlayServicesComponents I guess.

florian-magina commented 2 years ago

Hi @moljac, any news on this issue ? Thanks a lot for your feedback.

janwiebe-jump commented 2 years ago

I assume I have 'mitigated' this issue, by disabling the Cast notification when running on Android 12, by passing null into the CastMediaOptions.Bulder's SetNotificaiitonOptions() in my CastOptionsProvider. Maybe this is of some help to anybody here.

However, I prefer an update of the bindings to the latest version to be able to use the notification as well.

Unfortunately the sample above doesn't work.

janwiebe-jump commented 2 years ago

It looks like #582 updates the Cast Framework to version 21.0.1. @moljac anything we can do to speed this up?

gjlozano commented 1 year ago

works only with netstandard 2.0, i'm working with 2.1

ivs88 commented 1 year ago

Same issue, can't use Cast