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:
- Create a TWA project
- Pair with Firebase, add cloud messaging component
- Create a class extending FirebaseMessagingService and onMessageReceived method
- Delcare the service in AndroidManifest
- 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:
- Created 4 years ago
- Comments:17 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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.
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.