Questioner
Questioner

Reputation: 2541

Android notifications: onMessageReceived not called

First of all, it's not because the app is in the background.

The notifications are sent with a data message payload. In the Play console it says the messages are 'Acknowledged', so they're reaching the device. For most users, the onMessageReceived method is called, but for a minority, it isn't. Why would this be?

AndroidManifest:

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

MyFirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
...
}

Upvotes: 5

Views: 9525

Answers (7)

ChandraShekhar Kaushik
ChandraShekhar Kaushik

Reputation: 397

Have you used below service in manifest file?

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

if not then use it create a FirebaseIDService which extends FirebaseInstanceIdService implements its methods. If it still doesn't work then there's something wrong with the setup please folow https://www.survivingwithandroid.com/2016/09/android-firebase-push-notification.html to setup firebase into your project.

If still no response then you can use the Tomin's second method.

Updated:- Now you don't need to use above service tag inside your manifest because It's deprecated. Now you can implement the overriden method onNewToken(String token) in your FirebaseMessagingService and update your token.

Upvotes: 1

Martin Zeitler
Martin Zeitler

Reputation: 76569

the docs for "Handling Messages" state:

To receive messages, use a service that extends FirebaseMessagingService.

Your service should override the onMessageReceived and onDeletedMessages callbacks.

It should handle any message within 10 seconds of receipt. After that, Android does not guarantee execution, and could terminate your process at any time.

If your app needs more time to process a message, use the Firebase Job Dispatcher.

the implementation of onMessageReceived should benchmark the execution time and dispatch a Job with the dispatcher, from the RemoteMessage - in case the execution time should be above 10 seconds (on certain occasions). the docs for Override onMessageReceived even show it alike that.

always dispatching a job would generally work around the possibility to have the associated process terminated, no matter how long it may take to return. onMessageReceived most likely is being called, but terminated.

Upvotes: 1

Shadab K
Shadab K

Reputation: 1755

This happens when you send the message in general and not to specific device

Take the fcm token, and send the message to the "Single Device option".

This works.

Upvotes: 0

Tomin B Azhakathu
Tomin B Azhakathu

Reputation: 2686

This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.

When the intent is launched you can use them

getIntent().getExtras();

to retrieve a Set that would include any data sent along with the notification message.

SECOND METHOD

Override handleIntent(Intent intent) method in your FirebaseMessagingService class.

handleIntent() method is called every time whether the app is in the foreground, background or killed state.

For more on notification message see docs.

Upvotes: 2

Jayesh
Jayesh

Reputation: 131

I have two solutions for this:

1.Upgrade your firebase to version compile com.google.firebase:firebase-messaging:10.2.1
2.Override handleIntent(Intent intent) method in your FirebaseMessagingService class.

handleIntent() method is called everytime whether app is in foreground, background or killed state.

Upvotes: 1

Chandan Bhandari
Chandan Bhandari

Reputation: 217

the issue must be with the firebase token registration. have you logged the token generation in the below function:

public class TixDoFirebaseIntentService extends FirebaseInstanceIdService {
    @Override
    public void onTokenRefresh() {
        Common.log("FirebaseToken :"+FirebaseInstanceId.getInstance().getToken());
    }
}

there may be a case that user had installed the app but haven't opened it yet, therefore resulting in no generation of the token.

Upvotes: 1

Michele La Ferla
Michele La Ferla

Reputation: 6884

One of the main reasons why this could be happening is because the particular user has changed his login details or is using a different account when using the app. In this particular case, the particular device token would be receiving the notification, but since he is using a a different account, the notification is not displayed. One way to go around this issue is to clear the user's device token each time the user logs out and saved the newly created one, when a user logs in again. This way the device token is kept updated and the user will receive the notifications.

Upvotes: 3

Related Questions