FCM通知消息有助于使应用程序服务保持活动状态,但FCM数据消息却不能

时间:2019-04-04 17:09:06

标签: firebase firebase-cloud-messaging android-8.0-oreo foreground-service cordova-plugin-fcm

自从我实施FCM数据消息以来,我一直在通过调查我的应用程序服务来使我的应用程序前台服务保持活动状态,但是我发现该应用程序处于非活动状态,当我执行该操作时不会发生正在发送FCM通知消息。

这是我用来捕获数据消息的代码:

public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    // Cordova Callback
    FCMPlugin.sendPushPayload(data);

    Intent resultIntent = new Intent(this , FCMPluginActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0 , resultIntent,PendingIntent.FLAG_ONE_SHOT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        mBuilder = new Builder(this, "0");
    }else{
        mBuilder = new Builder(this);
    }

    mBuilder.setSmallIcon(getApplicationInfo().icon)
        .setContentTitle(title)
        .setContentText(messageBody)
        .setAutoCancel(false)
        .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){

        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel("0", "Playback status", importance);
        notificationChannel.setShowBadge(false);

        assert mNotificationManager != null;
        mNotificationManager.createNotificationChannel(notificationChannel);
    }

    assert mNotificationManager != null;
    mNotificationManager.notify(0, mBuilder.build());
}

如何通过使用FCM数据消息使我的应用程序前台服务在Oreo上保持活动状态?

1 个答案:

答案 0 :(得分:0)

观看Google news about battery usage restrictions后,我通过将代码与Foreground Service混合成功修复了它,看来FCM数据消息不足以使我的应用程序继续运行。