Уведомление об отмене не работает

Отмена уведомлений работает, когда у меня одно устройство. Но когда я добавляю еще одно устройство с другим идентификатором, это не отменяет никаких уведомлений. Я не понимаю, что делаю неправильно.

Вот код

private void cancelNotification(RemoteMessage remoteMessage) 
{
    int id = Integer.parseInt(remoteMessage.getData().get("id"));
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(id);
}

private void sendNotification(RemoteMessage remoteMessage) 
{
    Intent intent = new Intent(this, Activity.class);
    intent.putExtra("url",remoteMessage.getData().get("url"));
    PendingIntent pintent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent , 0);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification notificationBuilder = new Notification.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.splash_launcher))
            .setSmallIcon(R.drawable.splash_launcher)
            .setContentTitle(remoteMessage.getData().get("title"))
            .setContentText(remoteMessage.getData().get("text"))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pintent)
            .build();

    int id = Integer.parseInt(remoteMessage.getData().get("id"));
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, notificationBuilder);
}
1
0
161
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

В показанном вами коде нет ничего плохого. Чтобы отменить уведомление, которое вы ранее отправили, вы должны вызвать функцию cancel с тем же идентификатором, который вы использовали для вызова функции notify. Если ваш код не работает, RemoteMessage, который вы передаете своей функции cancelNotification, имеет идентификатор, отличный от идентификатора RemoteMessage, который вы передаете своей функции sendNotification.

Да, это была проблема на стороне сервера. Я передал код идентификатора уведомления своему отправителю foreach и изменил его на зацикленный код

rnod 31.07.2018 18:46

Другие вопросы по теме