Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在从应用程序取消firebase通知_Android_Firebase Cloud Messaging - Fatal编程技术网

Android 正在从应用程序取消firebase通知

Android 正在从应用程序取消firebase通知,android,firebase-cloud-messaging,Android,Firebase Cloud Messaging,我正在发送以下有效负载以进行通知: var message = { data: { user: sendData.from, body: sendData.message, sentto: sendData.to, gcm_username: sendData.from }, android: { priority: 'high', notification: { title: 'Ye

我正在发送以下有效负载以进行通知:

var message = {
    data: {
    user: sendData.from,
    body: sendData.message,
    sentto: sendData.to,
    gcm_username: sendData.from
    },
    android: {
        priority: 'high',
        notification: {
            title: 'Yeni mesaj aldınız',
            body: sendData.from + ' size bir mesaj gönderdi',
            tag: sendData.from,
            sound: 'default',
            channelId: 'messages'
            }
        },
    token: deviceToken
 };
但有时,当用户从应用程序图标打开应用程序时,我必须取消某些通知

我尝试了以下代码:

    @RequiresApi(api = Build.VERSION_CODES.M)
    public Notification deleteNotificationByTag(String senderCode) {
        NotificationManager notificationManager = (NotificationManager) mApplication.getSystemService(Context.NOTIFICATION_SERVICE);
        StatusBarNotification[] barNotifications = notificationManager.getActiveNotifications();
        for(StatusBarNotification notification: barNotifications) {
            Log.d(TAG, "getActiveNotification: " + notification.getTag());
            if (notification.getTag() != null && notification.getTag().equals(senderCode)) {
                notificationManager.cancel(notification.getId());
            }
        }
        return null;
    }

但是
notification.getId()
方法总是返回0值。所以取消不起作用。如何修复它?

您可以使用标签取消通知,如下所示:

public void cancel (String tag, int id);

因此,即使id为0,如果标记是唯一的,它也应该可以工作。

一个选项是发送数据消息并手动创建id为的通知,您可以根据需要取消消息。

我正在从服务器发送通知,而不是在本地创建。您可以在负载中添加类型,并检查负载的类型。如果有,请删除它。您可以通过传递假ID使用上述方法,然后通过假ID取消该通知。可能会这样work@TolgayToklar我知道您是从服务器发送的,但firebase会给您一条远程消息,我猜您可以使用NotificationManager显示该消息。当时我要求您使用发送时间创建id。如果你想在显示之前取消,那么我搞错了。不,如果应用程序没有运行(前台或后台),android没有调用RemoteMessage服务,它只是自己处理通知。但是人们说如果手机处于打瞌睡模式,不会调用onMessageReceived。根据文档,网络访问在睡眠模式下暂停,也不会发送非数据通知。您可以使用高优先级通知。您可以在此链接上检查瞌睡检查表和瞌睡限制