For Xamarin.Android, when the closed app receives a notification, it triggers HandleIntent(). I want to specify the target (intent) page after opening a background notification, like when receiving a foreground notification when the app is already open.
How do you specify in the HandleIntent() the intent page to deeplink to a different part of the app, instead of the background notification opening to the app homepage? Or is there a way to OnResume() the app and capture the Message.Data values then deeplink to another page?
using System;
using Android.App;
using Android.Content;
using Android.Media;
using Android.Support.V4.App;
using Firebase.Messaging;
namespace Appname
{
[Service(Name = "com.mydomainname.appname.MyFirebaseMessagingService")]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
internal static readonly string CHANNEL_ID = "alerts_channel";
public override void OnNewToken(string token)
{
base.OnNewToken(token);
}
public override void OnMessageReceived(RemoteMessage message)
{
if (message.Data.Count > 0)
{
// NOTIFICTION AS DATA WITH ID
Android.Util.Log.Debug(TAG, "From: " + message.From);
Android.Util.Log.Debug(TAG, "Title: " + message.GetNotification().Title);
Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
Android.Util.Log.Debug(TAG, "Data: " + message.Data["EId"]);
SendNotification(message.GetNotification().Title, message.GetNotification().Body, message.Data["EId"]);
}
else
{
// NOTIFICATION WITHOUT DATA
Android.Util.Log.Debug(TAG, "From: " + message.From);
Android.Util.Log.Debug(TAG, "Title: " + message.GetNotification().Title);
Android.Util.Log.Debug(TAG, "Notification Message Body: " + message.GetNotification().Body);
SendNotification(message.GetNotification().Title, message.GetNotification().Body, null);
}
}
public override void HandleIntent(Intent intent)// this method will fire when the app in background/closed state, and the foreground before calling OnMessageReceived()
{
base.HandleIntent(intent);
string strTitle = "";
string strBody = "";
string strEId = "";
if (intent.Extras != null)
{
foreach (var key in intent.Extras.KeySet())
{
var value = intent.Extras.GetString(key);
switch (key)
{
case "gcm.notification.title":
strTitle = value;
break;
case "gcm.notification.body":
strBody = value;
break;
case "EId":
strEId = value;
break;
}
}
}
}
void SendNotification(string messageTitle, string messageBody, string strEId)
{
Intent intent;
if(strEId == null)
{
intent = new Intent(this, typeof(MainActivity));
}
else
{
intent = new Intent(this, typeof(pageEvent));
intent.PutExtra("EID", strEId);
}
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0 /* Request code */, intent, PendingIntentFlags.OneShot);
var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
var notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetSmallIcon(Resource.Mipmap.ic_launcher_round)
.SetContentTitle(messageTitle)
.SetContentText(messageBody)
.SetAutoCancel(true)
.SetSound(defaultSoundUri)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0 /* ID of notification */, notificationBuilder.Build());
}
}
}