1

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?

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • "Please note that i have also tried by changing Notification sound in my code as well as in web server's json format", can you share the notification json ? – Saurabh Kataria Apr 02 '19 at 06:05
  • [How to call on message received in background](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase), have you looked into this? – Saurabh Kataria Apr 02 '19 at 06:07
  • @SaurabhKataria, please look in to the edited question, now i have posted my json of data payload. – partha sarathy Apr 02 '19 at 06:29
  • So i tried with your code, and its running properly, can you debug the data while sending notifications from server? For me onMessageRecieved is called everytime, whether its killed on is in background, and i am getting data payload everytime. – Saurabh Kataria Apr 02 '19 at 06:49
  • are you facing this issue on every device or some specific device ? – akshay_shahane Apr 02 '19 at 07:11
  • @SaurabhKataria, Now it perfectly works, but i didn't do anything on my code, but i don't know why it was not worked previously... Anyway, thanks for your valuable support guys. – partha sarathy Apr 02 '19 at 07:27
  • @akshay_shahane, I faced the problem with all the devices, but now it works... – partha sarathy Apr 02 '19 at 07:28
  • Hello guys, but some FCM messages delivers after much time. – partha sarathy Apr 02 '19 at 07:29
  • yeah , sometimes notifications take time, but its ok. That's normal behaviour. FCM is very good for notifications. @parthasarathy good you figured out. Cheers. – Saurabh Kataria Apr 02 '19 at 07:34

0 Answers0