Java 当应用程序被终止时,如何停止接收通知?

Java 当应用程序被终止时,如何停止接收通知?,java,android,firebase,push-notification,firebase-cloud-messaging,Java,Android,Firebase,Push Notification,Firebase Cloud Messaging,当我的应用程序被终止时,我需要不接收通知,但我没有找到任何这样的代码 void sendNotification(String messageBody, String icon, String titulo) { String channelId = "General"; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Notification

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

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());
    }
我可以在前台后台接收通知,但当我关闭应用程序时,它们仍然会到达

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

}
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());
    }
这是我的发送通知方法

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”, “通知”:{ “头衔”:“头衔”, “正文”:“****正文****”, “可变内容”:真 },

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());
    }
    "data": {
        "url": "https://upload.wikimedia.org/wikipedia/commons/8/80/Coiled_Galaxy.jpg",
        "title": "title",
        "body": "****body****."
    }


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

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

在后台和前台,如果它应该工作,但当应用程序被关闭时,它不应该收到notifications@AlexisOsorio你已经测试过了吗?如果没有,你应该试试。当应用程序在后台时,并不意味着应用程序被杀死了。被杀死需要很短的时间OS@AndroidDev.OP说他希望通知是di当应用程序位于前台或后台时显示。但不希望在应用程序被终止时显示通知。是的,我希望在应用程序位于前台或后台时显示通知。但不希望在应用程序被终止时显示通知。@Alexisorio,我建议您能够创建一个新的应用程序从
Application
class(如果没有)扩展的类method,您调用
registerReceiver
方法注册接收器。然后,在
FirebaseService
onMessageReceived
方法中,当您收到通知时,您发送一个广播接收器。我希望这会有所帮助。数据有效负载将工作。您需要为通知设置高优先级。什么类型的f您正在发送的有效负载?是否为仅数据有效负载?您是否也可以发布您尝试发送的firebase有效负载?我希望在应用程序位于前台或后台时显示通知。但不希望在应用程序被终止时显示通知。{“收件人”:“令牌”,“通知”:{“标题”:“标题”,“正文”:“****正文****。”,“可变内容”:true},“数据”:{“url”:“,“标题”:“标题”,“正文”:“****正文****”}”
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());
    }