In a xamarin forms project, I have added Firebase plugin to my project to do push notification.
The plugin works well in my IOS project. I can see the data in the output and when the app is in background the notification pops up.
My problem:
In my android project I can see in output console the data send on Firebase platform, but when the app is in background the pop up does not appear and I have this error:
[FirebaseMessaging] Unable to log event: analytics library is missing
[AndroidRuntime] FATAL EXCEPTION: Firebase-PNFirebaseMessagingService
[AndroidRuntime] Process: com.xxxx, PID: 11631
[AndroidRuntime] java.lang.IllegalArgumentException: com.LeafWords: 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:375)
[AndroidRuntime] at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
[AndroidRuntime] at android.app.PendingIntent.getActivity(PendingIntent.java:444)
[AndroidRuntime] at android.app.PendingIntent.getActivity(PendingIntent.java:408)
[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.zzc.run(Unknown Source:2)
[AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
[AndroidRuntime] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
[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:1012)
Thread finished: <Thread Pool> #2
I have followed many links but without success:
My code :
My Android Files : MainActivity
internal static MainActivity Instance { get; private set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
FirebasePushNotificationManager.ProcessIntent(this, Intent);
}
My Firebase file : MainApplication
[Application]
public class MainApplication : Application
{
public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
//Set the default notification channel for your app when running Android Oreo
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
//Change for your default notification channel id here
FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel";
//Change for your default notification channel name here
FirebasePushNotificationManager.DefaultNotificationChannelName = "General";
}
//If debug you should reset the token each time.
#if DEBUG
FirebasePushNotificationManager.Initialize(this, true);
#else
FirebasePushNotificationManager.Initialize(this,false);
#endif
//Handle notification when app is closed here
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
};
}
}
In my app.xaml.cs :
public App()
{
InitializeComponent();
MainPage = new LInPage();
}
// Firebase Notifcation
CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
{
System.Diagnostics.Debug.WriteLine($"MyTOKEN : {p.Token}");
};
CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Received");
foreach (var data in p.Data)
{
System.Diagnostics.Debug.WriteLine($"Mydata {data.Key} : {data.Value}");
}
};
CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
{
System.Diagnostics.Debug.WriteLine("Opened");
foreach (var data in p.Data)
{
System.Diagnostics.Debug.WriteLine($"Opended {data.Key} : {data.Value}");
}
};
}
Thanks for your help