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
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.
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.