0

I'm using Firebase Notifications. For test, I send notifications from the Firebase Console. But in my MainActivity I can't get data, which I set in the console.

Method onMessageReceived(RemoteMessage msg) in MyFirebaseService doesn't get called (if the method didn't get called - data transfer to MainActivity in Intent). But I don't see this data in Intent.

For example: enter image description here And in debug: enter image description here

AL.
  • 36,815
  • 10
  • 142
  • 281
Artem
  • 4,569
  • 12
  • 44
  • 86
  • 1
    Sorry. I'm a bit confused. Shouldn't you be using the [RemoteMessage.getData()](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage.html#getData()) to get the data payload? Also, do post the codes as text instead of images. Do include your `onMessageReceived()`. – AL. Sep 28 '16 at 08:21
  • @AL. - yes - it possible only if method onMessageReceived() called – Artem Sep 28 '16 at 08:25
  • 1
    I think onMessageReceived() won't be called if the app is in background. Did you check with the app in foreground ? – Mridul S Kumar Sep 28 '16 at 08:43
  • @ArtemShevchenko is this line of code attained after being redirected by the notification? – Al Wld Sep 28 '16 at 08:44
  • @MridulSKumar on my device onMessageReceived() never called. And I ask about another situation! – Artem Sep 28 '16 at 08:45
  • 1
    Is the onMessageReceived() called when the app is running ? – Mridul S Kumar Sep 28 '16 at 08:48

1 Answers1

0

If you use the console to send notification, it is not considered as payload, so if the application is running in background, the method onMessageReceived() is not called. But if the app run in foreground it does.

Here is a curl command to send a notification via curl as payload, you have to use a shell to use it.

curl -X POST --header "Authorization: key=<APIKEY>" --Header "Content-Type:
application/json" https://fcm.googleapis.com/fcm/send -d '{"to":"<YourToken>","data":{"type":"notification","message":"Hello, it works"}}'                     

**Note: remove the <>.

Your onMessageReceived() method looks like this to get the data.

 @Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
 try {
       String remote_type = remoteMessage.getData().get("type");
        if (remote_type.equals("notification")){
            String message = remoteMessage.getData().get("message");
            Log.d("ReceivedData", message);
        }
    }catch(NullPointerException e){ 
    }
Rocé Tarentula
  • 653
  • 1
  • 6
  • 15