1

I'm trying to get The data from the Notification when the App is Killed or in background here is what I've did : php side :

 $msg = array
      (
    'body'  => $msg,
    'title' => $title,
    'click_action' => "OPEN_ACTIVITY_1"

      );
$fields = array
        (
            'to'        => $token,
            'notification'  => $msg
        );


$headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

in my luncher Activity :

if(getIntent().getExtras()!=null){
   Strig msg = getIntent().getExtras().getString("body");
   String title =getIntent().getExtras().getString("title");
   Log.i(TAG,"Message "+msg);
   Log.i(TAG,"Title " +title);

 }

it's Returning null for both msg and title .. here is the code for onMessageReceived()

public void onMessageReceived(RemoteMessage remoteMessage) {
    clickAction= remoteMessage.getNotification().getClickAction();
    NotificationHelper h = new NotificationHelper(this);
    Map<String,String >data = remoteMessage.getData();
    title=data.get("title");
    msg=data.get("body");
    Log.i(TAG, "onMessageReceived: "+clickAction);
    h.createNotificationWithId(msg,title,clickAction);
}
AbdoHurbli
  • 11
  • 1
  • 6

1 Answers1

0

The issue you are facing has to do with the structure of the JSON payload.

See this question here for more examples.

The upshot is that if you have the 'notification' key and object value, you will not be able to handle the onMessageReceived in the background.

//Will not work in your situation
{
  "notification":{
    "title": "hello",
    "body": "this is body"
  },
  "to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
}

While this one will work because notification is absent, but the 'data' key is present.

//This will work for your situation
{
  "data":{
    "title": "hello",
    "body": "this is body"
  },
  "to": "c5iuJ7iDnoc:APA91bG6j2c5tiQ3rVR9tBdrCTfDQYxkPwLuNFWzRuGHrBpWiOajR-DKef9EZEEVKA-kUBfXVcqHT-mClYfad06R_rBjhRZFKVdBL7_joXE5hFEwR45Qk8wgQdia2b-LmjI1IheFGZS8"
}

One other thing, if you have both the 'notification' and 'data' keys, it will default to the first example and you will not be able to manually handle it in the onMessageReceived

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78