如果应用程序处于活动状态,则无法在频道上 post 通知

Failed to post notification on channel if application is active

我正在尝试集成 firebase 通知,但在尝试处理收到的通知时出现 "failed to post notification on channel" 错误。

只有当应用程序在前台时才会出现此问题。

如果它在后台,则可以毫无问题地显示通知。

我正在尝试 post 到频道 ID“12345”。我不确定这是否可能是问题所在,因为我没有创建频道,但如果这是问题所在,如果应用程序在后台,它是如何工作的?

我错过了什么?

我没有使用 Android 8,我实际上找到了一个应该在这种情况下用于创建频道的片段:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        }


    Intent intent = new Intent(this, ActivityMain.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // 0 is request code
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);



    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("FCM Message")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    //.setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // 0 is id of notification
    notificationManager.notify(0, notificationBuilder.build());

您需要创建通知渠道:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});  
notificationManager.createNotificationChannel(notificationChannel);

如果 Firebase 通知 不包含数据负载,Firebase 会在您的应用程序处于后台时自动处理通知。
这就是它在后台工作的原因。 请参阅此参考资料:https://firebase.google.com/docs/cloud-messaging/android/receive

我上面的逻辑有一个我错过的错误。问题出在 >= 26 的版本中,因为我创建了一个通道,然后没有将通知发送到正确的通知管理器,而是创建了一个新的通知管理器实例(查看问题代码的最后两行)。

这是更正后的逻辑:

private void sendNotification(String messageBody) {

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // create channel in new versions of android
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_ID, Constants.NOTIFICATION_CHANNEL_NAME, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        notificationManager.createNotificationChannel(notificationChannel);
    }


    // show notification
    Intent intent = new Intent(this, ActivityMain.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // 0 is request code
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID)
                    .setSmallIcon(R.drawable.icon_contact_purple)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    //.setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

    // 0 is id of notification
    notificationManager.notify(0, notificationBuilder.build());

}