Handling FCM notifications with notification payload, when the app is in background/killed?

I am using AppCenter to send notifications for my Xamarin.Forms application. AppCenter is sending these notifications through Firebase-Cloud-Messaging service to Android devices. I need to handle incoming notifications without the user clicking the notification.

I already have read this SO question and the accepted answer and as it says to handle notifications when the app is killed or in background, we have to send the firebase notification with the data payload. But with AppCenter you can't send messages with 'data' payload (https://github.com/microsoft/appcenter/issues/87).

So I have tried adding a Service which extends the FirebaseMessagingService and onMessageReceived(RemoteMessage remoteMessage) is only called when the app is in the foreground.

Now I'm looking for a way to handle notifications with a custom-service or something similar. In this SO question (https://stackoverflow.com/a/47312925/5278005), someone has already mentioned that they already implemented it.

@annihil we ended up having to create our own implementation of a BroadcastReceiver and JobIntentService to receive the FCM messages and explicitly pulling out the intent filters which sent the messages to the FCM classes shipped w/ their SDK – Jake Hall Jan 30 '18 at 11:58

I have been working with Xamarin.Forms and just a newbie for android development. So is it possible to do it without FirebaseMessagingService?

Answers

  • AnandmauryaAnandmaurya INMember ✭✭
    edited June 24

    @tharindu_mp Without service class as you mention "FirebaseMessagingService" notification will not work in Xamarin Android . Second thing You need to implement BroadcastReceiver in proper way for handle new OS above 7. And add IntentFilter dynamically for BroadcastReceiver.

  • tharindu_mptharindu_mp Member ✭✭

    @Anandmaurya Do you mean is it necessary to have a service class extending FirebaseMessaginService class to handle notifications?
    And what would be the intent filter for the broadcast receiver in this case?

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    what would be the intent filter for the broadcast receiver in this case?

    Add the following in AndroidManifest.xml.

    <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
    

    See the tutorial: https://firebase.google.com/docs/cloud-messaging/android/receive#edit-the-app-manifest

  • tharindu_mptharindu_mp Member ✭✭

    @yelinzh Can't register the MESSAGING_EVENT intent filter with a Broadcast receiver. (I have tried this one earlier)
    see this: https://stackoverflow.com/a/44287962/5278005

  • AnandmauryaAnandmaurya INMember ✭✭
    edited June 24

    @tharindu_mp Yes , We need to use services for getting the notification in background. Use this link

    https://docs.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-push-notifications-android-gcm

    It will work for Android OS below version 8 and If you want to run this in above OS version (8 or 9). So only we need to change in Broadcast Receiver Intent Filter and notification Chanel. Add dynamically Intent Filter . eg.

    [BroadcastReceiver]
    [IntentFilter(new[] { "GET_NOTIFICATION_INTENT" })]
    public class NotificationReceiver : BroadcastReceiver    
    

    NotificationReceiver is the class which is registering the notification in main class. Also add dynamic filter in main class.

    _notificationIntentFilters.AddAction("GET_NOTIFICATION_INTENT");

    lf still you have any question let me know...

Sign In or Register to comment.