【问题标题】:Azure push notification not closing after click when app is open应用打开时单击后 Azure 推送通知未关闭
【发布时间】:2021-09-21 10:48:33
【问题描述】:

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

主活动

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

【问题讨论】:

    标签: android xamarin.forms


    【解决方案1】:

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

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

    之所以显示通知是因为你用OnMessageReceived方法发送本地通知,解决问题,点击后不要忘记手动关闭。

    参考

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

    【讨论】:

    • 感谢您的回复。我不知道点击后如何手动关闭它。
    • 能否分享ShowNotification方法中的代码?
    • 我已将其添加到我的问题中。请检查
    • 更改NotificationManager 标志,参考stackoverflow.com/a/22218277/8187800
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    相关资源
    最近更新 更多