1

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:

  1. The tutorial
  2. install : Xamarin.AndroidX.Work.Runtime
  3. This link

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
hk77
  • 59
  • 8

1 Answers1

2

This should be the PendingIntent created by the nuget packages your project refer to didn't use FLAG_IMMUTABLE or FLAG_MUTABLE.

First of all, did you use the newest version of this package? If not, you can try to update it to the last one.

In addition, you can refer to this issue about Android 12 Java.Lang.IllegalArgumentException: 'com.infinity.estrats: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified for this nuget package. And the workaround in it is installing the Xamarin.AndroidX.Work.Runtime version 2.7.0 and he said only version 2.7.0 worked for his project.

Or you can also check the open issue about Android 12 notification crashes application, and you can try the workaound in it:

Install the following nuget packages:

Xamarin.Firebase.Common 120.0.5

Xamarin.Firebase.Iid 121.1.05

Xamarin.Firebase.Messaging 122.0.05

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Many thanks install the Libraries solves the probelm, Now I have this error : [FirebaseMessaging] Missing Default Notification Channel metadata in AndroidManifest. Default value will be used. – hk77 Jul 10 '23 at 22:58
  • Did you search the error message? There are many threads about this error. @hk77 – Liyun Zhang - MSFT Jul 11 '23 at 01:22
  • You can check this thread about [W/FirebaseMessaging: Missing Default Notification Channel metadata in AndroidManifest. Default value will be used](https://stackoverflow.com/questions/47453899/w-firebasemessaging-missing-default-notification-channel-metadata-in-androidman). @hk77 – Liyun Zhang - MSFT Jul 11 '23 at 01:24
  • I have not found it – hk77 Jul 18 '23 at 20:06
  • I am creating a new post to be more clear thanks for this case – hk77 Jul 18 '23 at 20:07