0

I know that this question is so old and there are a lot of answers on it, but I have not found my decision.

I want to update widget with firebase-cloud-messaging, but Firebase method onMessageReceived() is not calling when app is in background.

I've tried to change notification to data as in the most of the answers here, but it not helps me: onMessageReceived() is not calling when app is even in foreground.

So, my Firebase JSON is:

{ "registration_ids": [ "ids" ], 
"priority": "high", 
"content_available": true, 
"data": {
    "id_request": "1229",
    "unreadedAnnouncements": "4",
    "requestsCount": "9",
    "survaysCount": "5", 
    "sound":"default", 
    "title":"test", 
    "message": "test", 
    "body": "test" } }

If it matters, my Firebase-dependencies:

implementation 'com.google.firebase:firebase-core:10.2.1'
implementation 'com.google.firebase:firebase-messaging:10.2.1'
implementation 'com.google.firebase:firebase-auth:10.2.1'

Maybe there are some new solutions? On the SO there are the most of solutions from 2016.

Neuron
  • 1,020
  • 2
  • 13
  • 28
  • can u please revisit this thread https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background – Solaiman Hossain Oct 30 '18 at 09:15
  • @msh.nayan and what? Intent is not my solution (I want to update widget immediately). handleIntent is deprecated. And third solution is here – Neuron Oct 30 '18 at 09:20

2 Answers2

0
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    super.onMessageReceived(remoteMessage);

    if (remoteMessage.getData() != null)
        sendNotification(remoteMessage);
}

then you can parse data like this:

    private void sendNotification(RemoteMessage remoteMessage)
    {
       Map<String, String> data = remoteMessage.getData();
       String title = data.get("title");
       String content = data.get("content");
       // your code
    }
0

The solution that is helped me: In the Firebase I've just changed field registration_ids to to.

Final Firebase JSON:

{ "to": [ "ids" ], 
"priority": "high", 
"content_available": true, 
"data": {
    "id_request": "1229",
    "unreadedAnnouncements": "4",
    "requestsCount": "9",
    "survaysCount": "5", 
    "sound":"default", 
    "title":"test", 
    "message": "test", 
    "body": "test" } }
Neuron
  • 1,020
  • 2
  • 13
  • 28