【问题标题】:Failed to post notification on channel if application is active如果应用程序处于活动状态,则无法在频道上发布通知
【发布时间】:2018-01-11 18:27:09
【问题描述】:

我正在尝试集成 Firebase 通知,但在尝试处理收到的通知时出现“无法在频道上发布通知”错误。

此问题仅在应用程序位于前台时发生。

如果在后台,通知显示没有问题。

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

我错过了什么?

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

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());

【问题讨论】:

  • 发布生成通知的代码。
  • 我知道您目前没有使用您发布的 sn-p 创建频道。如果您确实使用过它,则需要进行更正。最后,添加对NotificationManager.createNotificationChannel(...)的调用。
  • @BobSnyder 我刚刚添加了其余的代码。 if 部分未在版本低于 8 的情况下执行,这就是在我的模拟器中执行的内容。我错过了什么?
  • 您表明您在 API 级别

标签: android firebase firebase-notifications


【解决方案1】:

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

这里是更正的逻辑:

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());

}

【讨论】:

  • 谢谢。有数百个类似的代码不起作用,包括官方指南,但这最终对我有用。不知道为什么被否决。
  • @AdamMendoza 也击败了我......但不幸的是,似乎有人在 stackoverflow 中唯一的任务是查看问题和答案并对其进行投票。仍然希望在其中看到一个功能,我可以看到谁投了反对票,并要求至少澄清他们为什么这样做。
【解决方案2】:

您需要创建通知渠道:

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 的版本怎么办?这正是我有上述 if 的原因。 NotificationChannel 创建方法需要版本 26。
  • 这个答案正确解释了后台行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多