【问题标题】:FCM notification messages help to keep app services alive, but FCM data messages don'tFCM 通知消息有助于使应用服务保持活力,但 FCM 数据消息却不能
【发布时间】:2019-04-04 17:09:05
【问题描述】:

自从我实现 FCM 数据消息以来,我一直试图通过查看我的应用程序服务来让我的应用程序前台服务在 Oreo 上保持活动状态,但我发现应用程序变得不活动,当我没有发生这种情况时正在发送 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 上保持活跃?

【问题讨论】:

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


    【解决方案1】:

    在观看Google news about battery usage restrictions 之后,我通过将我的代码与前台服务混合成功地修复了它,似乎 FCM 数据消息不足以让我的应用程序保持活力。

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2017-03-16
      • 2020-09-21
      • 2016-12-08
      • 1970-01-01
      • 2017-09-23
      相关资源
      最近更新 更多