0

Notification data is received when the Android application is background or foreground, but notification data is not received when it is terminated.

 @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            String tit = remoteMessage.getData().get("title");
            String body = remoteMessage.getData().get("body");
         
            sendNotification(tit, body); 
        }
    }
    private void sendNotification(String body, String title) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK  
                | Intent.FLAG_ACTIVITY_SINGLE_TOP   
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);  
   
        intent.putExtra("title", title);
        intent.putExtra("body", body);
        startActivity(intent);

    }
}

This is my code. What code should I add if I receive notification data when the app is shut down?

chan
  • 1
  • 1
  • are you sending a data or notification payload ? – Taki Feb 18 '22 at 01:54
  • Yes, the notification is coming, but if you click on the notification and enter, the notification data will not be received. – chan Feb 18 '22 at 02:02
  • what do you mean but the notification data will not be received ? – Taki Feb 18 '22 at 02:03
  • data: JSON.stringify({"registration_ids" : test, "notification": {"title":"=====Notification title","body":"=====Notification body"}, "data":{"title" : "000data title000", "body": "000data body000"}} ) This notification does not include data: title, body values. – chan Feb 18 '22 at 02:08
  • Since you have already invoked the notification once and it is received , it means that data was successfully fetched from server , upon clicking the notification , just pass your data as params and do something with it – Taki Feb 18 '22 at 02:09
  • What is the method of transferring it to the parameters? – chan Feb 18 '22 at 02:13
  • What you could for example is saving the data in sharedpreferences and upon clicking the notification and opening mainactivity , get data from sharedpreferences , or you could save data in local db temporarily – Taki Feb 18 '22 at 02:15

0 Answers0