I am using azure push notification for my Xamarin forms app. Its working fine. But in Android when app is open, If I click my notification its not closing. If app is background its closing correctly.
MainActivity
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
ProcessNotificationActions(intent);
CreateNotificationFromIntent(intent);
LoadApplication(new App PushNotificationFirebaseMessagingService.PushNotificationPageName, PushNotificationFirebaseMessagingService.PushNotificationAppName, PushNotificationFirebaseMessagingService.Notification));
}
This is Dependency service for notitification
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
var msgData = message.Data;
var NotificationMessage = msgData["action"];
var NotificationObj = JsonConvert.DeserializeObject<NotificationData>(NotificationMessage);
ShowNotification(message.GetNotification().Body, message.GetNotification().Title, NotificationObj.Notification[0].OpenPage.ToString(), NotificationObj.AppName.ToString(), NotificationMessage);
}
Show Notification Method
public int ShowNotification(string message, string title, string pageName, string appName,string NotificationMessage)
{
if (!channelInitialized)
{
CreateNotificationChannel();
}
_messageId++;
Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("title", title);
intent.PutExtra("message", message);
intent.PutExtra("OpenPage", pageName);
intent.PutExtra("AppName", appName);
intent.PutExtra("NotificationMessage", NotificationMessage);
var pendingIntent = PendingIntent.GetActivity(mContext, 0, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, ChannelId)
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.addovation_icon)
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);
Notification notification = builder.Build();
_manager.Notify(_messageId, notification);
return _messageId;
}