Zero
Zero

Reputation: 194

How to handle notifications when app is closed with FCM?

When my app is closed I get new notifications from FCM, but when I tap on it my app will crash. When my app is in background or foreground it is okay, then they will open the right page for the notification. I dont call SendNotification, because I dont want to have local notifications when the app is in foreground.

My fcm service:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
    const string TAG = "MyFirebaseMsgService";
    public override void OnMessageReceived(RemoteMessage message)
    {
        Log.Debug(TAG, "From: " + message.From);
        MessagingCenter.Send(App.CurrentApp, "ReceivedNotification");
    }

    void SendNotification(string messageBody)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Android.App.Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.ic_launcher)
            .SetContentTitle("FCM Message")
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }
}

Notification handling in OnCreate:

protected override void OnCreate(Bundle bundle)
    {


        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);
        Forms.Init(this, bundle);
        ImageCircleRenderer.Init();

        IsPlayServicesAvailable();

        if (Intent.Extras != null)
        {
            NotificationSendObject notifcation = new NotificationSendObject();
            foreach (var key in Intent.Extras.KeySet())
            {
                switch (key)
                {
                    case "object_id":
                        notifcation.objectId = Intent.Extras.GetString(key);
                        break;
                    case "notification_id":
                        notifcation.notificationId = Intent.Extras.GetString(key);
                        break;
                    case "object_type":
                        notifcation.objectType = Intent.Extras.GetString(key);
                        break;
                }
            }
            if (notifcation.notificationId != null)
            {
                MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation);
            }
        }


        SecureStorageImplementation.StoragePassword = "*****";

        LoadApplication(new App());

    }

Upvotes: 1

Views: 3500

Answers (1)

Zero
Zero

Reputation: 194

I find a solution, the Intent.Extras must be called after the LoadApplication method.

Fixed:

protected override void OnCreate(Bundle bundle)
{


    TabLayoutResource = Resource.Layout.Tabbar;
    ToolbarResource = Resource.Layout.Toolbar;

    base.OnCreate(bundle);
    Forms.Init(this, bundle);
    ImageCircleRenderer.Init();

    IsPlayServicesAvailable();

    SecureStorageImplementation.StoragePassword = "*****";

    LoadApplication(new App());

    if (Intent.Extras != null)
    {
        NotificationSendObject notifcation = new NotificationSendObject();
        foreach (var key in Intent.Extras.KeySet())
        {
            switch (key)
            {
                case "object_id":
                    notifcation.objectId = Intent.Extras.GetString(key);
                    break;
                case "notification_id":
                    notifcation.notificationId = Intent.Extras.GetString(key);
                    break;
                case "object_type":
                    notifcation.objectType = Intent.Extras.GetString(key);
                    break;
            }
        }
        if (notifcation.notificationId != null)
        {
            MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation);
        }
    }

}

Upvotes: 1

Related Questions