如何将通知发送到“杂项”通知渠道?

20

问题描述

在Android O中,当我尝试发送通知时,必须指定要发送的NotificationChannel。如果我使用旧方法(不设置任何通道),像这样NotificationCompat.Builder(this),则无法显示Notification。同样适用于无效通道,如NotificationCompat.Builder(this, "invalid")NotificationCompat.Builder(this, "")。 当我通过Firebase Cloud Messaging发送通知,并且我的应用程序处于后台状态而没有指定notification channel时,它将成为"Miscellaneous"通道中的通知。 当我尝试在前台执行相同操作时,上述内容将不起作用,创建名称为"Miscellaneous"和id为"{package}.MISCELLANEOUS"的通知渠道,然后通过它发送也不行。这时会发生以下情况:

Screenshot from my app's settings

我想知道的

如何像FCM那样发送通知,而不需要频道,以便它可以落在常规的"杂项"频道中?

这种方法的示例

如上所述,FCM通知就是一个例子,但是例如Gmail也使用杂项频道。那么我该如何使用呢?

Screenshot of Gmail's notification channels

我认为,如果杂项频道平时无法使用,他们会将其删除。

简短描述

为什么这段代码没有向“杂项”通知渠道发送通知,实际上它没有发送任何通知(只有在Android O上,该代码才能在较低版本的Android上工作)。

(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(1, NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
                    .setSmallIcon(R.drawable.small_icon)
                    .setContentTitle(URLDecoder.decode("Title", "UTF-8"))
                    .setContentText(URLDecoder.decode("Text", "UTF-8"))
                    .setColor(ContextCompat.getColor(applicationContext, R.color.color))
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setContentIntent(PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT))
                    .build())

创建通知渠道的步骤:
  1. 构建一个通知渠道对象,其ID在您的应用程序包内是唯一的。
  2. 配置通知渠道对象,包括所需的初始设置,例如警报声音,以及可供用户查看的可选描述。
  3. 将通知渠道对象提交给通知管理器。https://developer.android.com/preview/features/notification-channels.html
- Jon Goodwin
@creativecreatorormaybenot,你找到解决方案了吗?你尝试过刚发布的最终版Android 8(与开发者预览版相比)吗? - Vit Khudenko
@Arhimed 我没有尝试过最终的Android 8,我想。 - creativecreatorormaybenot
2个回答

14

请参考Sca09的答案,因为情况并非总是如此。 - Gp2mv3
谢谢你,我爱你。与notifee一起工作。 - undefined

7

正如在另一个答案中所述,Android系统创建的默认通道的id是fcm_fallback_notification_channel,但要注意,系统只有在必须管理第一条推送通知时才会创建该通道。因此,如果您在FirebaseMessagingService类的扩展中管理所有通知,则可能会出现通道不存在的错误,例如:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=fcm_fallback_notification_channel pri=-2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

我的建议是在创建通知之前检查默认频道是否存在,如果不存在则创建它:

private void createDefaultNotificationChannel() {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     NotificationManager notificationManager = getSystemService(NotificationManager.class);

     if (notificationManager.getNotificationChannel("fcm_fallback_notification_channel") != null) {
       return;
     }

     String channelName = getString(R.string.fcm_fallback_notification_channel_label);
     NotificationChannel channel = new NotificationChannel("fcm_fallback_notification_channel", channelName, NotificationManager.IMPORTANCE_HIGH);
     notificationManager.createNotificationChannel(channel);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接