0

i am trying to integrate fcm to receive remote notifications and messages using Plugin.FirebasePushNotification in my xamarin.forms app. i set my fcm as i created a project and downloaded the google-services.json, and i added it to my android project and set its build action to GoogleServicesJson. i also added a MainApplication Class:

[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 = "DefaultChannel";

                //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, new NotificationUserCategory[]
            {
           
            new NotificationUserCategory("request",new List<NotificationUserAction> {
                new NotificationUserAction("Accept","Accept",NotificationActionType.Default,"check"),
                new NotificationUserAction("Reject","Reject",NotificationActionType.Default,"cancel")
            })

            }, true);
#else
                FirebasePushNotificationManager.Initialize(this,new NotificationUserCategory[]
            {
            new NotificationUserCategory("message",new List<NotificationUserAction> {
                new NotificationUserAction("Reply","Reply",NotificationActionType.Foreground),
                new NotificationUserAction("Forward","Forward",NotificationActionType.Foreground)

            }),
            new NotificationUserCategory("request",new List<NotificationUserAction> {
                new NotificationUserAction("Accept","Accept",NotificationActionType.Default,"check"),
                new NotificationUserAction("Reject","Reject",NotificationActionType.Default,"cancel")
            })

            },false);
#endif

            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
            {
                System.Diagnostics.Debug.WriteLine("NOTIFICATION RECEIVED", p.Data);


            };



        }
    }
}

and in my MainActivity i added this:

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);

        }

in my App.xaml.cs i added the following

public App()
        {
            InitializeComponent();

            MainPage = new MainPage();

            CrossFirebasePushNotification.Current.Subscribe("all");
            CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh;
        }

        private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine($"Token: {e.Token}");
        }

i added everything i saw in this tutorial: https://www.youtube.com/watch?v=7w2q2D6mR7g&t=479s and tried the sample found in the plugin documentation. i still don't get any notifications when the app is in the background, and in the foreground, i could only read the message body in the output in my visual studio, but no notification is received. what am i doing wrong? thanks in advance.

rana hd
  • 355
  • 3
  • 18
  • You can refer to the answer in this case: [Xamarin forms - Android Firebase notification does not pop up when app is in background](https://stackoverflow.com/questions/76645239/xamarin-forms-android-firebase-notification-does-not-pop-up-when-app-is-in-bac). – Jianwei Sun - MSFT Jul 17 '23 at 08:32

0 Answers0