1
votes

In my Android app I need to handle receiving of Firebase push notification messages, so that I can manipulate their ids (so that all notifications display rather than replacing each other) and so that I can display an app icon (setting it with a meta-data element in Android manifest doesn't work.)

I've creating a messaging service to handle receiving messages:

[Service (Name = "com.rpr.mobile.droid.LocalyticsFirebaseMessagingService")]
    [IntentFilter (new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class LocalyticsFirebaseMessagingService : FirebaseMessagingService {
        private static int notificationId = 0;

        public override void OnMessageReceived (RemoteMessage message) {
            if (!message.Data.ContainsKey ("ll")) {
                base.OnMessageReceived (message);
            } else {
                var body = message.GetNotification ().Body;
                if (!String.IsNullOrEmpty (body)) {
                    var mainIntent = new Intent (this, typeof (IntentActivity));
                    var deepLinkUrl = "";
                    if (message.Data.TryGetValue ("ll_deep_link_url", out deepLinkUrl))
                        mainIntent.SetData (Android.Net.Uri.Parse (deepLinkUrl));
                    var launchIntent = PendingIntent.GetActivity (this, 1, mainIntent, PendingIntentFlags.UpdateCurrent);

                    var builder = new NotificationCompat.Builder (this)
                        .SetSmallIcon (Resource.Drawable.logo_blue_small)
                        .SetContentTitle (GetString (Resource.String.application_name))
                        .SetContentText (body)
                        .SetStyle (new NotificationCompat.BigTextStyle ().BigText (body))
                        .SetContentIntent (launchIntent)
                        .SetDefaults (-1)
                        .SetAutoCancel (true);

                    var notificationManager = NotificationManagerCompat.From (this);
                    notificationManager.Notify (notificationId++, builder.Build ());
                }
            }
        }
    }

And I've defined it in the application section of my Android manifest:

<service android:name="com.rpr.mobile.droid.LocalyticsFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

However, when I receive a push notification this code is never called, even when the app is in the foreground. I had similar code that worked fine for GCM push notifications, but I'm having no luck for Firebase. What am I missing?

1

1 Answers

0
votes

You have to add the below code to your android manifest file inside application section:

<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
    <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>

and in your class you have to add like this

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MessagingService : FirebaseMessagingService

and this is the code to get the token

[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class IDService : FirebaseInstanceIdService

also you have to add the google-services.json file to your project with the build action GoogleServicesJson