FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

Android 12 notification crashes application

Open R3muSGFX opened this issue 3 years ago • 11 comments

🐛 Bug Report

I started to develop a new Xamarin Forms application and use this plugin to be able to handle the FCM push notifications. I tested this under Android 10 (version 29) and it worked nicely on both 12 and under. The problem emerged when I moved the target version to Android 12 (version 31). Everytime I call the FCM endpoint to send a push notification to my device it sends out an error, but this only happens and application is in background or stopped at all. It is worth mentioning that I don't even get any notification banner on Android 12, almost instantly after sending the request from postman the Android application crashes, while the same code-behind on iOS works smoothly. The error message is as follows:

[AndroidRuntime] FATAL EXCEPTION: Firebase-PNFirebaseMessagingService [AndroidRuntime] Process: package.name.ommited, PID: 1425 [AndroidRuntime] java.lang.IllegalArgumentException: package.name.ommited: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. [AndroidRuntime] 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. [AndroidRuntime] at android.app.PendingIntent.checkFlags(PendingIntent.java:382) [AndroidRuntime] at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) [AndroidRuntime] at android.app.PendingIntent.getActivity(PendingIntent.java:451) [AndroidRuntime] at android.app.PendingIntent.getActivity(PendingIntent.java:415) [AndroidRuntime] at com.google.firebase.messaging.zza.zzh(Unknown Source:124) [AndroidRuntime] at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:57) [AndroidRuntime] at com.google.firebase.iid.zzg.run(Unknown Source:4) [AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) [AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) [AndroidRuntime] at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.6.0:2) [AndroidRuntime] at java.lang.Thread.run(Thread.java:920)

After analyzing a bit the plugin backend I've seen a lot of packages that are outdated, mostly 2019. Is there any chance that an update with the latest packages would come?

I would appreciate any other recommendations to workaround against this issue, I would need to publish these applications to the appstores very soon.

Thank you in advance,

Expected behavior

Receive notification banner without problems.

Reproduction steps

  1. Have a basic application that can receive FCM push notification.
  2. Launch the application on target framework 31 (Android 12 S)
  3. Put the application in background
  4. Launch a notification either from firebase console or from postman with additional data.

Configuration

Version: 3.4.1

  • [ ] :iphone: iOS
  • [X ] :robot: Android
  • [ ] :checkered_flag: WPF
  • [ ] :earth_americas: UWP
  • [ ] :apple: MacOS
  • [ ] :tv: tvOS
  • [ ] :monkey: Xamarin.Forms

R3muSGFX avatar Jan 31 '22 16:01 R3muSGFX

same problem here

tomzipl avatar Feb 07 '22 13:02 tomzipl

Same issue, is there any known workarounds for this? I have added the below to my AndroidManifest.xml which fixes the deployment issue I had.. but I am still receiving this error as described..

	<receiver tools:node="merge"
	          android:name="crc6494e14b9856016c30.PNFirebaseMessagingService"
	          android:exported="false">
		<intent-filter>
			<action android:name="com.google.firebase.MESSAGING_EVENT" />
		</intent-filter>
	</receiver>

jmbowman1107 avatar Feb 09 '22 21:02 jmbowman1107

On which devices are u receiving the error guys? On <= android 12 while targeting api level 31?

themronion avatar Feb 15 '22 14:02 themronion

On which devices are u receiving the error guys? On <= android 12 while targeting api level 31?

I am running on a device with Android 12, with target api level 31. On the same device with android 12 and target api level 31 there is no issue.

R3muSGFX avatar Feb 16 '22 15:02 R3muSGFX

I was actually using the non firebase version of this plugin (still uses firebase for Android), and after I made some changes to allow it work at all for Android 12 in a fork. I also started receiving this same error.

The fix for this was anywhere there was: PendingIntentFlags.UpdateCurrent

I changed it to PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable

eg: PendingIntent.GetActivity(context, requestCode, resultIntent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);

jmbowman1107 avatar Feb 17 '22 00:02 jmbowman1107

I was actually using the non firebase version of this plugin (still uses firebase for Android), and after I made some changes to allow it work at all for Android 12 in a fork. I also started receiving this same error.

The fix for this was anywhere there was: PendingIntentFlags.UpdateCurrent

I changed it to PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable

eg: PendingIntent.GetActivity(context, requestCode, resultIntent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable);

@jbowmanp1107 if you will look closely to the plugin repository and changes you would notice that what you have mentioned is already done.

R3muSGFX avatar Feb 17 '22 08:02 R3muSGFX

To get around this issue I installed the nuget packages

Xamarin.Firebase.Common 120.0.5 Xamarin.Firebase.Iid 121.1.05 Xamarin.Firebase.Messaging 122.0.05

The issue does not seem to be with this package but rather the old dependent packages

bigpjo avatar Apr 22 '22 16:04 bigpjo

@bigpjo Thanks a lot for the tip, I was struggling with it and just installed what you specified and now everything works!

Luisalvarenga avatar Jun 03 '22 15:06 Luisalvarenga

@Luisalvarenga @bigpjo I'm experiencing this issue as well but non of the suggestions above work for me. I'm getting a FCM token but when I push to it, nothing happens. Not in application log or anything visible with adb logcat. Any suggestions on how you have it working at the moment?

jtorvald avatar Oct 14 '22 10:10 jtorvald

@jtorvald Sometimes the token registration takes a few minutes (my expirience), before it is saved in firebase backend: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessaging#public-taskstring-gettoken If you init the plugin like this FirebasePushNotificationManager.Initialize(this,true); a new token is created every time you start your app. You could set resettoken to false and try again. If this still not work implement your own PushNotificationHandler:

internal class CustomPushNotificationHandler : DefaultPushNotificationHandler {
public override void OnReceived(IDictionary<string, object> parameters) {
base.OnReceived(parameters);
}
}

FirebasePushNotificationManager.Initialize(this, new CustomPushNotificationHandler(), false); and set a breakpoint at base.OnReceived(parameters);

AlleSchonWeg avatar Oct 14 '22 11:10 AlleSchonWeg

@jtorvald what @AlleSchonWeg said is completely true and make sure your device has a proper internet connection to ensure it.

Luisalvarenga avatar Oct 14 '22 14:10 Luisalvarenga