簡體   English   中英

應用程序被殺死時如何停止接收通知?

[英]How to stop receiving notifications when app is killed?

當我的應用程序被殺死時,我不需要收到通知,但我沒有找到任何可以接收的代碼。

我可以在前台后台接收通知,但是當我終止應用程序時它們仍然會到達。

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

{ "to": "token", "notification": { "title": "title", "body": "****body****.", "mutable_content": true },

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


}

它可能在這里重復。 重要提示:如果應用程序在后台或被終止,那么要收到通知,有效負載必須刪除notification屬性

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM