Modifying background notifications from FCM in Android

I followed the Xamarin examples for adding notifications to the Android side of my app using FCM.

See: https://developer.xamarin.com/guides/android/application_fundamentals/notifications/remote-notifications-with-fcm/

I am able to receive both background and foreground messages but the example only shows how to customize the foreground notifications.

I was wondering how to customize the background notifications as they do not create a sound or vibration by default. I only see an example on how to change the icon.

Thanks

Posts

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai
    edited January 2017

    If it's in the Foreground, you would need to customize the notification in the OnMessageReceived override inside your custom FirebaseMessagingService. Please note there is a big difference between the limitations you have in the Background rather than the Foreground. Such as when it's in the Background, you get a default notification. If it's in the Foreground, you can customize via the method above.

    Here's a Java sample showing this:

    https://github.com/firebase/quickstart-android/blob/master/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java

    This sample is in Xamarin as well here:

    https://github.com/xamarin/GooglePlayServicesComponents/blob/master/firebase-messaging/samples/FirebaseMessagingQuickstart/MyFirebaseMessagingService.cs

    Otherwise you should be able to view most of these options in the documentation:

    https://firebase.google.com/docs/cloud-messaging/concept-options

  • Nando779Nando779 USMember

    Thanks for the references. I finally noticed the FCM Notification console has an option to enable the sound on it's messages so doing that enable the background notification to have sound. We will be sure to add this option to the JSON string our server code will be passing.

    I am now having an issue with receiving the notification's payload via Intent.Extras...

  • ManjitManjit USMember

    But Background notification showing multiple notification in the notification bar rather than stacking them in one notification with count of notification.

    How can I show like following :
    you have 3 new messages from abc app

  • loglog ITMember

    @Manjit said:
    But Background notification showing multiple notification in the notification bar rather than stacking them in one notification with count of notification.

    How can I show like following :
    you have 3 new messages from abc app

    any news?
    I'm able to receive background and foreground but my firebase messaging service trap only foreground so I can use SetGroup only if my app is presented.
    When app is closed or backgrounded I receive many single notifications and I'm not able to understand which is the code to intercept "system tray" notifications to my app.
    I tried to find solution on google but it seems there's no simple solution.
    I also tried to implement a broadcaster receiver but (maybe) I didn't find the right code.
    Any help will be appreciated.
    thanks

  • taitotaito USMember ✭✭

    @log said:

    @Manjit said:
    But Background notification showing multiple notification in the notification bar rather than stacking them in one notification with count of notification.

    How can I show like following :
    you have 3 new messages from abc app

    any news?
    I'm able to receive background and foreground but my firebase messaging service trap only foreground so I can use SetGroup only if my app is presented.
    When app is closed or backgrounded I receive many single notifications and I'm not able to understand which is the code to intercept "system tray" notifications to my app.
    I tried to find solution on google but it seems there's no simple solution.
    I also tried to implement a broadcaster receiver but (maybe) I didn't find the right code.
    Any help will be appreciated.
    thanks

    same thing. I don't find any solutions for handling notifications who are revived in the background.

  • loglog ITMember

    @taito said:
    same thing. I don't find any solutions for handling notifications who are revived in the background.

    send everything with 'data' in your json object and avoid 'notification'
    if you use 'notification' you will not be able to handle the received messages and everything will be a push
    with data you can use for both back and foreground messages the same function to manage your notification
    modify your webapi message sender function

    read this one from SO: stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase

    1) With this type of structure
    {
    "to": "/topics/my_topic",
    "data": {
    "my_custom_key" : "my_custom_value",
    "other_key" : true
    }
    }
    (only with "data" you will be able to intercept notification from code)

    2) With "notification" in your json
    {
    "to": "/topics/journal",
    "notification": {
    "title" : "title",
    "text": "data!",
    "icon": "ic_notification"
    }
    }
    (system will push notification)

  • taitotaito USMember ✭✭
    edited April 2017

    solved

Sign In or Register to comment.