I summarize the steps here, hopefully it helpful
STEP 1
Following FCM user guide. Make sure everything work properly by pushing a message from Firebase console.
STEP 2
Change your custom FirebaseMessagingService class as following:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
startActivity(new Intent(this, SplashActivity.class));
}
}
Now using Firebase console push messages again, you will found that onMessageReceived
only fires when your app is in the foreground. Because Firebase console is able to send Notification message
only. In order to send Data message
, we need step 3.
STEP 3
Install Google Advanced REST client
Input below params:
url: https://fcm.googleapis.com/fcm/send
method: POST
Raw header:
Content-Type: application/json
Authorization:key=YOUR_SERVER_KEY
Raw payload:
{
"to" : "YOUR_DEVICE_FCM_TOKEN",
"data" : {
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
},
}
YOUR_SERVER_KEY is available in the Firebase console > Settings pane > the Cloud Messaging tab
YOUR_DEVICE_FCM_TOKEN is FirebaseInstanceId.getInstance().getToken()
on onTokenRefresh()
NOTE
Starting UI without user interaction is bad UX, you might need to replace start activity with start service or something in the background. I just use activity for testing purpose, so it will be more visual than service.
Thanks @2ndgab for introducing to the Google Advanced REST client tool.