Search code examples
androidfirebasexamarin.formspush-notification

Xamarin forms firebase push notification edit the message content before display


I am new to xamarin forms and tryig to modify the notification content before displaying it. I am using data payload only.

Problem : I can see the modified content message and also the normal message( before modify content) also. This issue is when the app is in background, foreground I can see only modified message. How do I cancel the previous message, I want to show only the modified message.

I used this tutorial to display the firebase message using package Plugin.FirebasePushNotification

CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
{
   ModifyMsgAsync(this, p).Wait();
}            

ModifyMsgAsync(Context context, FirebasePushNotificationDataEventArgs message)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "123");
builder.SetContentTitle("Changed the title");
builder.SetContentText("Message body is changed");

builder.SetPriority(NotificationCompat.PriorityHigh);
builder.SetAutoCancel(true); //disappear after sometime
NotificationManagerCompat managerCompat = NotificationManagerCompat.From(context);
managerCompat.Notify(notification_id, builder.Build());
}

Solution

  • Actually the problem is, when app is in background system process the notification message using the default channel.

    Solution: The solution is after modifying the content delete the default channel to not to process the system handled message. this solve the issue.

    [Application]
        public class MKApplication : Application
        {
            
    
            public MKApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
            {
            }
    
            public override void OnCreate()
            {
                base.OnCreate();
    
                //Firebase.FirebaseApp.InitializeApp(this);
                
                //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";
    
                }
        }