Xamarin forms android: Old notifications are replaced by new notifications in foreground mode.

SreeeeSreeee INMember ✭✭✭✭✭

I have implemented push notification on xamarin forms android project using FCM. But when receive a new notification, old notifications are replaced by the new one. This is happening only when the app is in the foreground mode(when app is in open state). No such issues in background state or killed state. Multiple notifications are showing in background state and killed state.

Following is my notification payload, where webContentList is my message data.

{
 "to" : "dmtfiSvBBM0:APA91bFnHkamMSYgxPuiSfdvKnU8hD_mOqrWijnENNgXVSkSgo1ILH3-uKVCU7Ez2PXXOhtDoobIyKBf5UshVfTmvjSqHgXMRTsqguKCSTjIfGnXrVP-_cNFq2sisshZO-BcfkwKTl-I",
 "collapse_key" : "type_a",
 "notification" : {
      "body" : "This is body",
     "title": "Tech Team",
     "priority":"high",
     "content_available":true
 },
 "data" : {
    "webContentList": [
        {
            "webContentDefinitionId": 818084,
            "pageTitle": "CCD Grade 3-4",
            "pageKwd": "CCD Grade 3-4",
            "pageDesc": "CCD Grade 3-4",
            "siteId": 45,
            "pageCreatedTime": 1555145959428,
            "pageUpdatedDate": 1555927274279,
            "modifier": {
                "userId": 12944,
                "applicationId": 32,
                "username": "robert.downey",
                "email": "[email protected]",
                "firstName": "Robert",
                "lastName": "Downey"
            },
            "creator": {
                "userId": 12944,
                "applicationId": 32,
                "username": "robert.downey",
                "email": "[email protected]",
                "firstName": "Robert",
                "lastName": "Downey"
            }
        }
        ]
 },
  "ttl": 3600
}

I removed the collapse_key from the payload, but notifications are replacing in foreground mode.

Please suggest a solution for this issue?

Best Answer

  • GraverobberGraverobber ✭✭✭
    edited July 17 Accepted Answer

    Do you handle the foreground notification creation by yourself?
    If so, a classic mistake is to pass the same id over and over again when creating the local notification, for example like this
    mNotificationManager.Notify(1, mBuilder.Build());

    You need to pass a different ID for each notification

    mNotificationManager.Notify(new Random().Next(),mBuilder.Build());

Answers

  • GraverobberGraverobber Member ✭✭✭
    edited July 17 Accepted Answer

    Do you handle the foreground notification creation by yourself?
    If so, a classic mistake is to pass the same id over and over again when creating the local notification, for example like this
    mNotificationManager.Notify(1, mBuilder.Build());

    You need to pass a different ID for each notification

    mNotificationManager.Notify(new Random().Next(),mBuilder.Build());

  • SreeeeSreeee INMember ✭✭✭✭✭

    @Graverobber Thanks it worked for me :)

    When tapping the notification I need to show the corresponding message listing page as done in the ios. I am using the following code for handling the notification tap in android for background and killed state.

    if (Intent.Extras != null)
                {
                    foreach (var key in Intent.Extras.KeySet())
                    {
                        var value = Intent.Extras.GetString(key);
                        if (key == "webContentList")
                        {
                            if (value?.Length > 0)
                            {
                                LoadApplication(new App(value));
                            }
                        }
                    }
                }
    

    value contains the model data. In App.xaml.cs I have implemented the loading of message listing page based on the value. But this code not worked for foreground mode. So can you help me to implement the foreground mode notification tapping?

  • GraverobberGraverobber Member ✭✭✭

    Usually you would pass a PendingIntent which contains the activity to start when the user taps on the notification. Then from this Activity you can handle the data and send a message by messaging center as you did for iOS. As your app is running App was already loaded, therefore you can not (or should not?) call LoadApplication again. You can pass your MainActivity as PendingIntent and mark it as singleTop to avoid it being recreated.

    But just a thought, do you really need to display a notification when the app is in foreground?
    When the app is already running you can handle the notification however you want. Instead of displaying a notification you could just parse your value and send the message through Messaging Center, no inbetween step needed. But this depends on your application flow.

Sign In or Register to comment.