5

I migrated to Firebase Cloud Messaging and when first tried sending message I received a notification using the console in Firebase and then attempted to send another notification after a few minutes but i no longer receives another notification but in my firebase console it said that it was Completed

update

Here is my code

MainActivity

public class MainActivity extends Activity {
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 checkPlayServices();

 Log.i(TAG, "InstanceID token: " + FirebaseInstanceId.getInstance().getToken());

}
....
}

MyFirebaseInstanceIDService

public class MyFirebaseInstanceIDService  extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    sendRegistrationToServer(refreshedToken);
}
}

MyFirebaseMessagingService

public class MyFirebaseMessagingService  extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";


// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
// [END receive_message]

it seems that other devices receives it but the other devices doesn't received it.

natsumiyu
  • 3,011
  • 7
  • 23
  • 49

1 Answers1

2

Due to AdamK's comment asking if I send the second notification while in foreground, I found out that notification is only triggered when my app is in the background.

In the documentation it was said that in onMessageReceived(), If the application is in the foreground handle both data and notification messages in the onMessageReceived(). Therefore a notification will not be triggered automatically if the app is in foreground, so in order to do that, in your onMessageReceived() callback you need to trigger a notification yourself.

I call my own sendNotification() method inside my onMessageReceived():

sendNotification(remoteMessage.getNotification().getBody());

Which shows the notification even when the app is in the foreground.

natsumiyu
  • 3,011
  • 7
  • 23
  • 49
  • 1
    Properly tag this as answer. :) – AL. May 26 '16 at 05:36
  • 1
    I don't understand your solution. Do you mind elaborating? Do you know get it to see Notification in both foreground and background? My point of confusion is that if `onMessageReceived` is not called anyway, why does it matter that you call `sendNotification(remoteMessage.getNotification().getBody())` from inside `onMessageReceived`? Or are you making that call from somewhere else? – Nouvel Travay May 26 '16 at 22:37