【发布时间】:2016-12-23 05:56:01
【问题描述】:
只有当应用程序不在前台时,我才需要向用户显示通知。这是我的公共类 MyFirebaseMessagingService 扩展
FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(applicationInForeground()) {
Map<String, String> data = remoteMessage.getData();
sendNotification(data.get("title"), data.get("detail"));
}
}
需要实现applicationInForeground()方法
【问题讨论】:
-
你可以使用Activity的生命周期回调,当它到达BG/FG时向服务发送广播
-
您在应用程序中使用了多少活动?
-
FCM data-message(后台)和display-message(前台)有两种消息。您必须对两种消息使用不同的配置才能在后台或前台接收消息。看看这里stackoverflow.com/questions/37711082/…
-
看看 Sam 的答案:stackoverflow.com/questions/3873659 基于该答案的 Github 项目:github.com/seguri/GetForegroundActivity