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

Android 如果应用程序处于活动状态,则无法在通道上发布通知,android,firebase,firebase-notifications,Android,Firebase,Firebase Notifications,我正在尝试集成firebase通知,但在尝试处理收到的通知时,出现“无法在通道上发布通知”错误 此问题仅在应用程序位于前台时发生 如果是在后台,则会毫无问题地显示通知 我正在尝试发布到频道id“12345”。我不确定这是否是问题所在,因为我没有创建频道,但如果这是问题所在,如果应用程序在后台,它是如何工作的 我错过了什么 我没有使用Android 8,实际上我发现了一个片段,在这种情况下应该用来创建一个频道: if (android.os.Build.VERSION.SDK_INT >=

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

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

如果是在后台,则会毫无问题地显示通知

我正在尝试发布到频道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会在应用程序处于后台时自动处理通知。
这就是它在后台工作的原因。
请参阅此参考资料:

您需要创建通知频道:

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会在应用程序处于后台时自动处理通知。
这就是它在后台工作的原因。
请参阅此参考资料:

我上面的逻辑有一个错误,我遗漏了。问题出现在版本>=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());

}

我上面的逻辑有一个错误,我错过了。问题出现在版本>=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());

}

26以下的版本呢?这正是我拥有上述if的原因。NotificationChannel创建方法需要版本26。此答案正确解释了背景行为。26以下的版本如何?这正是我拥有上述if的原因。NotificationChannel创建方法需要版本26。此答案正确解释了背景行为。发布生成通知的代码。我知道您当前没有使用发布的代码段创建频道。如果你真的使用过它,就需要更正。最后,添加对@BobSnyder的调用。我刚刚添加了代码的其余部分。if部分不是在版本低于8的情况下执行的,这是在我的模拟器中执行的。我遗漏了什么?您表示您正在API级别<26的仿真器上运行。什么是API级别?发布生成通知的代码。我知道您当前没有使用发布的代码片段创建频道。如果你真的使用过它,就需要更正。最后,添加对@BobSnyder的调用。我刚刚添加了代码的其余部分。if部分不是在版本低于8的情况下执行的,这是在我的模拟器中执行的。我遗漏了什么?您表示您正在API级别<26的仿真器上运行。API级别是多少?谢谢。有数百个类似的代码不起作用,包括官方指南,但这最终对我起了作用。不知道为什么会被否决。@AdamMendoza也打败了我。。。但不幸的是,stackoverflow中似乎有一些人的唯一任务是查看问题和答案并对其进行否决投票。我仍然希望看到一个功能,在这里我可以看到谁投了反对票,并要求至少澄清他们为什么这样做。谢谢。有数百个类似的代码不起作用,包括官方指南,但这最终对我起了作用。不知道为什么会被否决。@AdamMendoza也打败了我。。。但不幸的是,stackoverflow中似乎有一些人的唯一任务是查看问题和答案并对其进行否决投票。我仍然希望看到这部电影中的一个特写,在那里我可以看到谁投了反对票,并要求至少澄清他们为什么投了反对票。