0

I have a example output from the server to push notification to an app which is shown below:

  {
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }

How do I store the contents in data when the app is in background as onMessageReceived is only triggered when the app in in foreground.

AL.
  • 36,815
  • 10
  • 142
  • 281
John
  • 411
  • 2
  • 8
  • 22

3 Answers3

1

As per the documentation, onMessageReceived() will be triggered when your app is foreground or background for notification and data pay load message. but, when your app is in background, notification message will be handled in notification tray. But, Data pay load message will be received as usual(via onMessageReceived()). No issues will be there. before that , you must implement the message handling logic on client application.

check the documentation

Noorul
  • 3,386
  • 3
  • 32
  • 54
  • I tried putting some print statement in the `onMessageReceived()` but it only triggers when my app is in foreground. – John Dec 23 '16 at 06:42
  • Yes. Even the log will not work on this stage. google does some tricks. you can get the information when you verify the FireBase classes. – Noorul Dec 23 '16 at 06:45
  • see this doc: https://firebase.google.com/docs/notifications/android/console-audience#backgrounded – Noorul Dec 23 '16 at 06:55
1

You can use getIntent().getExtras(); for fetching the intent while launching to get the intent.

more info here

eg:

  Bundle bundle = getIntent().getExtras();
  if (bundle != null) {
      if (bundle.containsKey("data")) {
      Intent intent = new Intent(mContext, ExpectedActivity.Class)
      intent.putExtras("PUSH_KEY",bundle.get("data").toString());
      startActivity(intent)
    }
  }

Place this code in your launcher activity.And this will navigate you to your expected activity even when the app is killed or is in background.

Also can you try changing your json like this

{
"data" : {
"body" : "great match!",
"title": "Portugal vs. Denmark" ,
"icon  : "myicon," 
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
},
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."
}
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53
  • seems like by removing the notification tag in the json did the trick. Source: https://code.google.com/p/android/issues/detail?id=219084 – John Dec 23 '16 at 07:21
  • What if there are multiple notifications? @sunil – John Dec 23 '16 at 07:23
  • @John Sry for the late response I was in vacation.How do you want to handle multiple notification ?. You can try with it PendingIntent . – Sunil Sunny Dec 28 '16 at 05:21
0

I've found the solution. It seems that I have to remove the notification part in the json when sending from local server to the firebase server. Then it will trigger the onMessageReceived if the app is on the background.

Source: https://code.google.com/p/android/issues/detail?id=219084

So what I did was remove the notification tag from the json and manually create my my notification. Then using @sunil's method to retrieve it from launcher activity.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    System.out.println("Remote Message: " + remoteMessage.toString());
    System.out.println("Token: " + FirebaseInstanceId.getInstance().getToken());
    System.out.println("From: " + remoteMessage.getFrom());
    sendNotification(remoteMessage.getData().toString());
    Log.d(TAG, "Message Data payload" + remoteMessage.getData());
}

private void sendNotification(String messageData) {
    Intent intent = new Intent(this, HomeActivity.class);
    intent.putExtra("data", messageData);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("DeliveryOptions")
            .setContentText("Test")
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}
John
  • 411
  • 2
  • 8
  • 22