Gui*_*ira 5 android android-intent firebase firebase-cloud-messaging
该FirebaseMessagingService有方法onMessageReceived(),我们应该哪个override来处理notifications,但是这只能在应用程序中Foreground.
要处理notifications应用程序在后台时,我曾经覆盖handleIntent,只是调用onMessageReceived().
在FirebaseMessagingService11.6.0中,该方法handleIntent成为最终的,据说,我无法像我一样覆盖它.
当我的应用程序在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) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
它不适合任何人覆盖handleIntent()。这就是为什么它被定为最终的原因。另外,您会注意到javadoc中完全缺少它- 这是故意的。
如果您想在任何情况下(前台和后台)处理消息,请使用onMessageReceived()。该方法的 javadoc说:
收到消息时调用。
当应用程序位于前台时收到通知消息时也会调用此函数。可以使用 getNotification() 检索通知参数。
这应该适用于数据消息,但不适用于从控制台发送的通知消息。通知消息具有不同的传递行为。请参阅有关消息类型以及如何处理它们的文档。
| 归档时间: |
|
| 查看次数: |
10175 次 |
| 最近记录: |