Search code examples
androidfirebasepush-notificationfirebase-cloud-messagingandroid-push-notification

Firebase Cloud Messaging (FCM) onMessageReceived never gets called in the service


I'm using firebase to send a push notification to Android devices. When I send a push with notification It works fine, the device shows a notification and clicking on the notification will send the user to the main activity and sends the data as intent extras as expected. this is my request for sending a push notification with notification. This is the body of my POST request to https://fcm.googleapis.com/fcm/send

{
"to" : "d_axTaq9LxM:APA91bH3Q-u0TEEWTTuihv-UKpEHy_J00gMsYdCvnRUdqaa1tN2VdI0AUyE0c8yOvFnZF6XQ4cORbUsTGe84YGsS4xkgDOI7tOE33eJUT8OYdFYRkSfeFntcs3zcd54BObcXxwAF1VFlK4gL1ByICkHnjXXmaiShZA",
    "collapse_key" : "type_a",
    "notification" : {
         "body" : "First Notification",
         "title": "Collapsing A"
    },
    "data" : {
        "body" : "First Notification",
        "title": "ALT App Testing",
        "key_1" : "Data for key one",
        "key_2" : "Hellowww"
    }
}

The above code works but when I remove the notification part and make a request like below it doesn't work any more and my service doesn't receive the data in my service. I don't want any notification, I just want my service to wake up and receive the data.

{
"to" : "d_axTaq9LxM:APA91bH3Q-u0TEEWTTuihv-UKpEHy_J00gMsYdCvnRUdqaa1tN2VdI0AUyE0c8yOvFnZF6XQ4cORbUsTGe84YGsS4xkgDOI7tOE33eJUT8OYdFYRkSfeFntcs3zcd54BObcXxwAF1VFlK4gL1ByICkHnjXXmaiShZA",
    "collapse_key" : "type_a",
    "data" : {
        "body" : "First Notification",
        "title": "ALT App Testing",
        "key_1" : "Data for key one",
        "key_2" : "Hellowww"
    }
}

The request is sent to FCM and gets a success response like this

{
    "multicast_id": 6489464663382515471,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1532347638201363%5a92300dcccfb49c"
        }
    ]
}

MyFirebaseService looks like this

public class MyFirebaseService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Push notification message data payload: " + 
remoteMessage.getData());
        }
    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);

        Log.e("alz", "new firebase token: " + s);
        Toast.makeText(this, "new firebase toekn: " + s, Toast.LENGTH_SHORT).show();
    }
}

And this is the definition of my service in the manifest file

    <service android:name=".fcm.MyFirebaseService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

I can't figure out what's the problem since I don't get any error. My push simply doesn't receive in onMessageReceived() method.

Any help or suggestion will be appreciated.


Solution

  • You forgot to add <intent-filter> of com.google.firebase.MESSAGING_EVENT in your MyFirebaseService

    Now we no need to use <intent-filter> of "com.google.firebase.INSTANCE_ID_EVENT Because FirebaseInstanceIdService is deprecated

    SAMPLE CODE

    <service android:name=".fcm.MyFirebaseService">
       <intent-filter>
              <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
     </service>