java - 当应用程序被杀死时如何停止接收通知?

标签 java android firebase push-notification firebase-cloud-messaging

当我的应用程序被终止时,我不需要收到通知,但我还没有找到任何这样做的代码。

我可以在前台后台接收通知,但当我终止应用程序时它们仍然会到达。

public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            try {
                Log.d(TAG, "Message Notification Body: " +  remoteMessage.getData().get("body"));
            }
            catch (Exception e){           
            }
        }

        //The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
        //You can change as per the requirement.
      try {
    //message will contain the Push Message
    String message = remoteMessage.getData().get("body");
    //imageUri will contain URL of the image to be displayed with Notification
    String imageUri = remoteMessage.getData().get("image");
    String titulo = remoteMessage.getData().get("title");
    String sidusuario = remoteMessage.getData().get("sidusuario");
    String idnotificacionusuario = remoteMessage.getData().get("idnotificacionusuario");

    sendNotification(message, imageUri,titulo);
    }
    catch (Exception e){
        e.printStackTrace();       
    }

}

这是我的sendNotification方法

void sendNotification(String messageBody, String icon, String titulo)
{

    String channelId = "General";
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setLargeIcon(getBitmapFromURL(icon))
                    .setContentTitle(titulo)
                    .setSmallIcon(R.drawable.logo_blanco_chapur)
                    .setContentText(messageBody)
                    .setStyle(new NotificationCompat.BigTextStyle())
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(getID(), notificationBuilder.build());
    }

{ “至”:“ token ”, “通知”: { “标题”:“标题”, " body ": "**** body ****。", “可变内容”:真 },

    "data": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/8/80/Coiled_Galaxy.jpg",
        "title": "title",
        "body": "****body****."
    }


}

最佳答案

它可能重复 here重要提示:如果应用程序处于后台或已被终止,则有效负载必须删除通知属性才能收到通知

enter image description here

关于java - 当应用程序被杀死时如何停止接收通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58704730/

相关文章:

android - 了解 firebase 的 createUser 函数(特别是 android 库)

java - 为什么我的 while 循环不会破坏任何可以帮助我的人?

java - 如何在RecycleView Multi ViewHolder中使用数组显示数据?

java - ArrayList<Object> 发送槽套接字

java - 如何在 Android (Java) 中以编程方式制作 TextView 多行

Android Firebase 和 Stripe 集成

java - 可作为具有参数交换的 OSGi 包执行

Java 8 Lambda 和 Streams...不是 "how",而是 "why"?

android - android中现有类中的java.lang.NoClassDefFoundError

angular - 在 Angular TestBed 中模拟 Firestore 集合