Actually, My application is to ring my phone from a remote server, So I decided to go with Firebase cloud messaging. I have written a media player to play for 5 seconds as below,
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//if app is in foreground
if (remoteMessage.getNotification().getBody().equalsIgnoreCase("deleteall"))
{
MediaPlayer player = MediaPlayer.create(this, R.raw.uniphone);
player.start();
}
//if app is in background
if(remoteMessage.getData().size() > 0)
{
MediaPlayer player = MediaPlayer.create(this, R.raw.uniphone);
player.start();
}
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
This code works fine when the app is in the foreground, but this doesn't work when the app is in the background or kill state.
I know that FCM's onMessageReceived will not be called when the app is in the background, instead, the notifications are shown with the help of the system tray.
When I googled about this problem, I came across with a solution that we have to send 'data message' instead of 'Notification message' with the fact that data message is allowed to call onMessageReceived of FCM.
Please note that I have also tried by changing Notification sound in my code as well as in the web server's json format, but nothing works.
my json as the data payload
{"to":"<my_fcm_token_here>",
"data": {
"somekey": "somevalue",
"somekey1": "somevalue1"
}
}
I also tried the data Message concept, but that too didn't work... I am just trying to fix it for the past 3 days and everything fails, please anyone help me to solve this issue.
Or else, is there any alternative for my application to ring my phone from the remote server other than FCM?