Understanding Push Notifications Lifecycle

In Mobile Development by Christian HissibiniLeave a Comment

Push notifications are a common feature in mobile apps and if you read my previous post or others you will easily understand how to receive them. Most walk-throughs stop at that point but what do you do after you receive them? As you will find out there are a few scenario’s in different platforms you need to accommodate.

Android

Android is the most interesting to handle push notifications, where its handy to have a few tricks up your sleeve. When you are in the foreground in your class which inherited GcmServiceBase, there is an OnMessageReceived method which is called when a push notification is received. When your app is backgrounded it will come through the MainActivity.cs.

androidnotificationlifecycle

In the foreground, you will need to code yourself or use a plugin like Toast Notifications for Xamarin and Windows to show a Snackbar or Notification. You must handle the display of the push notification yourself. When backgrounded, the user opens the app via clicking the notification, you must handle the response in the MainActivity, but while most examples might show you using it in the OnCreate, this actually isn’t a great place to use it.

In your MainActivity.cs set your LaunchMode to SingleTop. This means your MainActivity wont be created again when opened.

[Activity(Label = "Mobile App", Theme = "@style/MainTheme", LaunchMode = LaunchMode.SingleTop)]

Next use the OnNewIntent to handle the notification arguments passed through, instead of OnCreate.

protected override void OnNewIntent(Intent intent)
{
    if (intent != null)
    {
        var message = intent.GetStringExtra("message");

        if (!string.IsNullOrEmpty(message))
        {
              // Do something
        }
    }
 }

WakefulBroadcastReceiver

Unlike other platforms, if your Android app is closed, it will not receive Push Notifications. This is because Android receives push notifications via a service that the app launches. If the app is closed, then the service is not running and hence the app will not receive any push notifications. However, there is a way around this, by implementing a WakefulBroadcastReceiver.

First, add the WakeLock permission in your AndroidManifest.xml.

 <uses-permission android:name="android.permission.WAKE_LOCK" />

Now, add a WakefulBroadcastReceiver.

public class BroadcastReceiver : WakefulBroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var pm = PowerManager.FromContext(context);
        var wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, "GCM Broadcast Reciever Tag");
        wakeLock.Acquire();

        // Handle the notification here. You have an Intent and Context which will contain the information you are after.
        // ....

        // When you are finished
        sWakeLock.Release();
    }
}

iOS

In iOS, if the app is in the foreground, a different method is called than when in the app is in the background. ReceivedRemoteNotification is used when the app is backgrounded, DidReceiveRemoteNotification is called when the app is in the foreground.

iosnotificationlifecycle

If the app is in the foreground, it also won’t show a push notification, you have to manually either show an alert or show a notification of your own. For this you might want to look at the Toast Notifications for Xamarin and Windows plugin, or manually send an UNNotificationRequest (iOS 10+) if you want to roll your own.

UWP

Windows is the easiest platform, as a Toast Notification will be displayed if the app is in the foreground and when the app is backgrounded. When a user clicks on the Toast, the OnActivated function will be called in your App.xaml.cs.

uwptoastlifecycle

The args in here are from the launch argument in your toast if you placed any in the notification you sent.

Ref
https://docs.microsoft.com – https://xamarinhelp.com – https://blog.xamarin.com/

Leave a Comment