FCM通知消息有助于保持应用服务的活跃,但FCM数据消息却没有

问题描述 投票:0回答:1

我一直试图通过查看我的应用程序服务来保持我的应用程序前台服务在Oreo上运行,因为我正在实现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上运行?

firebase firebase-cloud-messaging android-8.0-oreo foreground-service cordova-plugin-fcm
1个回答
0
投票

看完qazxsw poi后我通过将我的代码与前台服务混合成功修复了它,似乎FCM数据消息不足以让我的应用程序保持活跃状态​​。

© www.soinside.com 2019 - 2024. All rights reserved.