Android notifications: onMessageReceived not called

随声附和 提交于 2020-06-25 21:15:49

问题


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) {
...
}

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/47159376/android-notifications-onmessagereceived-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!