androidfirebasefirebase-cloud-messagingprogressive-web-appstrusted-web-activity

MyFirebaseMessagingService.onMessageReceived() does not work


I am trying to "catch" firebase cloud messages sent to the app, and when I recieve(/click) one I want to execute something based on the "Custom data" key/value added in the firebase console.

My code right now looks like this:

MyFirebaseMessagingService:

package com.company.app;

import android.util.Log;

import androidx.annotation.NonNull;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "Notification";

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

        Log.i(TAG, "Receiving notification...");
        for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Log.d(TAG, "key, " + key + " value " + value);
        }
    }
}

AndroidManifest.xml:

        ...

        <!-- Firebase notification -->
        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">

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

    </application>

build.gradle (Module:app):

dependencies {

    //Google firebase
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.firebase:firebase-messaging:20.0.1'

    ...

My guess is that <service android:name=".MyFirebaseMessagingService" android:exported="false"> is not being executed, but I can't figure out why. I am not getting any errors. Other than this my notifications work without any issues.

Here's the console log if that's of any assisstance:

D/FA: Logging event (FE): notification_receive(_nr), 
Bundle[{
 ga_event_origin(_o)=fcm,
 message_device_time(_ndt)=0,
 message_type(_nmc)=display,
 message_name(_nmn)=B1 - TN18,
 message_time(_nmt)=1574177464,
 message_id(_nmid)=8950284200210511319}]

Any ideas?

Edit: the same code works in an otherwise empty app

Edit 2: problem is somehow caused by google's TWA LauncherActivity

Edit 3:

public class LauncherActivityTWA extends AppCompatActivity {

private TwaLauncher mTwaLauncher;
...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    this.mTwaLauncher.launch(getLaunchingUrl()); //this is causing the issue
}

Edit 4: for now I have solved this by switching over to WebView, I will try to get it working with TWA later. I still think this issue is somehow caused by TWA or some libraries associated with it

Edit 5: So I found a kind of a workaround - if you want to keep using TWA and FCM at the same time you'll have to work with Data Messages only, Notification Messages don't seem to trigger onMessageReceived even when app is in foreground


Solution

  • The problem only occurs when the received message is a notification message. It works as expected with data messages, even with TWA. More info about message types here.

    TL;DR: data messsages trigger the even always, notification messages only when app is in foreground.

    I don't know what's causing this issue, but I've already created an issue on GitHub about it, so maybe it'll get fixed in the future.