0
votes

I need to don't receive notifications when my app is killed but I have not found any code that does.

I can receive notifications in foreground and background but they still arrive when I kill the app.

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

}

This is my sendNotification method

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****."
    }


}
1
Data payload will be worked. You need to set priority high for notification.Piyush
What sort of payload are you sending? Is it a data-only payload? Can you post the firebase payload you are trying to send too?Derryl Thomas
i want the notifications to be displayed when the app is in foreground or background. But does not want the notification to be displayed if the app was killed.Alexis Osorio
{ "to": "token", "notification": { "title": "title", "body": "****body****.", "mutable_content": true }, "data": { "url": "upload.wikimedia.org/wikipedia/commons/8/80/Coiled_Galaxy.jpg", "title": "title", "body": "****body****." } }Alexis Osorio

1 Answers

0
votes

It maybe duplicates here. Important: if the app is in background or killed then to be received a notification the payload have to remove the notification property

enter image description here