skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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";
    
                }
        }
    

  2. It sounds like you’re sending a message with a notification key in there. When the user is not actively using the app, such messages are handled by the system – which means that your code has no way to control how the message is displayed.

    If you always want your code to handle the display of the message, send messages with only a data key in there.

    Also see the Firebase Cloud Messaging documentation on message types.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search