question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FirebaseMessagingService not working with TWA

See original GitHub issue

[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());
        }
    }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aguatnocommented, Jan 28, 2020

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.

0reactions
janfouscommented, Jan 28, 2020

Good to know it wasn’t caused by some bug in my code. I guess you can close this issue now, since it isn’t caused by a bug in your system. I will post a solution if I find one.

Thank you for your assistance and sorry for wasting your time with this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MyFirebaseMessagingService.onMessageReceived() does ...
The problem only occurs when the received message is a notification message. It works as expected with data messages, even with TWA.
Read more >
Receive messages in an Android app - Firebase - Google
To receive messages, use a service that extends FirebaseMessagingService. ... Any notification message that does not explicitly set the icon in the ...
Read more >
Standard Push Notification Integration for Android - Braze
Braze push notifications won't work until a Firebase Cloud Messaging token (FCM ... If you already have a Firebase Messaging Service registered, do...
Read more >
Enable push notifications for Android (optional)
If you encounter a problem when you do not receive your notifications in the application, then increase the priority of your FirebaseMessagingService over...
Read more >
Push Notification using FirebaseMessagingService not ...
i have succeeded in loading google-Service.json file (confirmed by the getting google App Id logged in output windows as suggested by above ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found