firebase-android-sdk: FirebaseMessagingService not working with TWA

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 3.5.2
  • Firebase Component: Cloud messaging(Database, Firestore, Storage, Functions, etc)
  • Component version: 20.0.1

[REQUIRED] Step 3: Describe the problem

FirebaseMessagingService doesn’t work properly when using TWA to create app. A notification sent from the Firebase console will get delivered correcly, however trying to do something like:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public void onMessageReceived(RemoteMessage remoteMessage) {
        ...
    }
}

just doesn’t work - the onMessageReceived method is never called. The same code works when using WebView instead of TWA.

Steps to reproduce:

  1. Create a TWA project
  2. Pair with Firebase, add cloud messaging component
  3. Create a class extending FirebaseMessagingService and onMessageReceived method
  4. Delcare the service in AndroidManifest
  5. Try to send a notification through the Firebase console. It will be received, but onMessageReceived won’t be executed

Relevant Code:

LauncherActivity

public class LauncherActivityTWA extends AppCompatActivity {
    private static final String TAG = "TWALauncherActivity";
    private LauncherActivityMetadata mMetadata;
    private TwaLauncher mTwaLauncher;
    private PwaWrapperSplashScreenStrategy mSplashScreenStrategy;
    private boolean mBrowserWasLaunched;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.mMetadata = LauncherActivityMetadata.parse(this);

        TrustedWebActivityBuilder twaBuilder = (new TrustedWebActivityBuilder(this, this.getLaunchingUrl())).setStatusBarColor(this.getColorCompat(this.mMetadata.statusBarColorId));
        this.mTwaLauncher = new TwaLauncher(this);
        this.mTwaLauncher.launch(twaBuilder, this.mSplashScreenStrategy, () -> {
            this.mBrowserWasLaunched = true;
        });  //.launch causes the incorrect behaviour, launch(Uri uri), understandably, behaves identically
    }
   ...

AndroidManifest.xml

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

MyFirebaseMessagingService

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "Notification";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        
        if (remoteMessage == null) {
            Log.d(TAG,"Message is null");
            return;

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG,"Message is not null");            
            handleNotification(remoteMessage.getNotification().getBody());
        }
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 17 (2 by maintainers)

Most upvoted comments

You’re welcome. I’ll be closing this issue now. If you want to continue the discussion just leave a comment here and we are happy to re-open this.