FirebaseMessagingService
有方法 onMessageReceived()
我们应该覆盖
来处理通知
,但这只是当应用程序处于 Foreground
时工作。
为了即使在应用程序处于后台时也能处理通知
,我过去常常覆盖handleIntent
,只调用onMessageReceived()
。
在 FirebaseMessagingService
11.6.0 中,handleIntent
方法成为 final方法,也就是说,我无法像以前那样覆盖它。
当我的应用程序在 11.6.0 中处于后台时,我应该如何处理通知?
public class NotificationsListenerService extends FirebaseMessagingService {
private static final String TAG = "NotificationsListenerService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
String notifyData = remoteMessage.getData().get("notifData");
if(notifyData.contains("|")){
String[] itens = notifyData.split("\\|");
notifyData = itens[0];
}
String notifyType = remoteMessage.getData().get("notifType");
String title = remoteMessage.getData().get("title");
String message = remoteMessage.getData().get("body");
if(!isAppInForeground(App.getContext())){
sendNotification(title, message, notifyData, notifyType);
}
}
@Override
public final void handleIntent(Intent intent) {
...
this.onMessageReceived(builder.build());
...
}
private void sendNotification(String messageTitle, String messageBody, String notifyData, String notifyType) {
...
}
//Detect if app is in foreground
private boolean isAppInForeground(Context context) {
...
}
}
最佳答案
它不适合任何人覆盖 handleIntent()
。这就是为什么它被定为 final 的原因。此外,您会注意到 javadocs 中完全没有它。 - 这是故意的。
如果您想在任何情况下(前台和后台)处理消息,请使用 onMessageReceived() . javadoc对于那个方法说:
Called when a message is received.
This is also called when a notification message is received while the app is in the foreground. The notification parameters can be retrieved with getNotification().
这应该适用于数据消息,但不适用于从控制台发送的通知消息。通知消息具有不同的传递行为。请参阅有关 message types 的文档和 how to handle them .
关于android - FirebaseMessagingService 11.6.0 处理 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47308155/