【发布时间】:2017-03-13 06:18:20
【问题描述】:
我对在我的应用程序上集成 FCM(Firebase 云消息传递)推送通知有点困惑。
通常,除了 Intent 服务,其他服务不会在中间停止。我通过扩展到 FirebaseMessagingService 创建了我的消息接收服务,如下所示
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "NotificationBean Body: " + remoteMessage.getNotification().getBody());
//This method is responsible for handling notification
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
}
并在清单上注册服务如下:
<service android:name=".service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
当应用程序处于活动状态并且也在后台运行时,此服务会运行。但是当应用程序不在后台时,服务不再运行。
我已经在 Main Activity 上注册了如下服务
@Override
protected void onResume() {
super.onResume();
// register GCM registration complete receiver
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
// register new push message receiver
// by doing this, the activity will be notified each time a new message arrives
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
// clear the notification area when the app is opened
NotificationUtils.clearNotifications(getApplicationContext());
}
@Override
protected void onPause() {
super.onPause();
}
让我知道我的代码有什么问题。 (可能不是)。为什么通知不在后台运行。我该如何解决这个问题?
提前致谢。
【问题讨论】:
-
我已经找到了解决这个问题的方法。该解决方案在华为手机上运行良好。但不适用于Xiomi RedMe note 3。解决方案链接在这里itechify.com/2016/02/01/…
-
我们不能以编程方式解决这个问题
标签: android push-notification google-cloud-messaging firebase-cloud-messaging