应用程序打开时单击后 Azure 推送通知未关闭

问题描述:

我正在为我的 Xamarin 表单应用程序使用 azure 推送通知.它的工作正常.但是在 Android 中,当应用程序打开时,如果我点击我的通知,它不会关闭.如果应用程序在后台正确关闭.

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.

主活动

    protected override void OnNewIntent(Intent intent)
{ 
     base.OnNewIntent(intent);
     ProcessNotificationActions(intent);
     CreateNotificationFromIntent(intent);
     LoadApplication(new App PushNotificationFirebaseMessagingService.PushNotificationPageName, PushNotificationFirebaseMessagingService.PushNotificationAppName, PushNotificationFirebaseMessagingService.Notification));
}

这是通知的依赖服务

   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);

    }

显示通知方法

        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;
    }

通知仅在应用程序处于后台或关闭时显示.

The notification only shows when the application is in background or closed .

当应用程序运行时,OnMessageReceived 会被触发,但我们看不到通知.

When the application is running , the OnMessageReceived would be triggered but we can't see the notification.

通知显示的原因是你在OnMessageReceived方法中发送Local Notification,解决问题,点击后不要忘记手动关闭.

The reason why the notification display is because you send Local Notification in OnMessageReceived method , to solve the problem , don't forget to dismiss it manually after clicking on it .

参考

https://stackoverflow.com/a/63037820/8187800.