1

I want to start a background service when I received a special type of message. For this I am doing the following

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(remoteMessage.getData().get("message").equals("something"){
              //start service
        }
    }
}

My problem is, it only works when app is foreground , but when the app is in background onMessageReceived doesn't trigger. Rather than it just show a notification .

What I want is to disable notification when app is in background , rather start a service.

I have looked other answers is SO, some of them suggest to accomplish this I need to send data-message . And I am sending data message from firebase console, but it is not working.

AL.
  • 36,815
  • 10
  • 142
  • 281
mSapps
  • 601
  • 9
  • 24
  • in your data message add one extra key-value pair so you can check that key in onMessageReceived() – Murli Prajapati Apr 09 '17 at 10:19
  • Just print message received and see if it is "data" or "notification". As far as I remember, if you send message from Firebase console, it will go as notification. – Dexter Apr 10 '17 at 04:16

1 Answers1

3

What is suggested is to send a data-only message payload. This is not possible using the Firebase Notifications Console.

When using the console, the message sent is treated as a notification message payload, and only adding Custom Data will the data payload be included. However, in that case, it still has both notification and data messages.

To send a data-only message payload, you'd need to build the request yourself through your own App Server or by using Postman or cURL

Graham
  • 7,431
  • 18
  • 59
  • 84
AL.
  • 36,815
  • 10
  • 142
  • 281