如何发送到 "Miscellaneous" 通知渠道?
How to send to "Miscellaneous" notification channel?
问题描述
当我尝试在 Android 中发送 Notification
O 时,我必须指定要发送到的 NotificationChannel
。
如果我像这样使用旧方法(不设置任何频道)NotificationCompat.Builder(this)
,Notification
将不会显示。
对于像这样 NotificationCompat.Builder(this, "invalid")
或 NotificationCompat.Builder(this, "")
的无效频道也是如此。
当我通过 Firebase Cloud Messaging 发送通知并且我的应用程序在后台运行且没有指定 通知渠道 时,它将是"Miscellaneous" 频道中的通知。
当我尝试在上面提到的前台做同样的事情时,将不起作用,也不会创建名称为 "Miscellaneous" 和 ID [=52 的通知渠道=]"{package}.MISCELLANEOUS" 然后通过它发送。当我这样做时,会发生以下情况:
我想知道的
如何在没有像 FCM 那样的渠道的情况下发送通知,所以它落在常规 "Miscellaneous"频道?
这个工作的例子
正如我上面提到的,FCM 通知 会发生这种情况,但是例如Gmail 也使用杂项渠道。 那么如何使用呢?
我相信他们会删除杂项频道,如果它通常无法使用的话。
更简短的描述
为什么这段代码没有向"Miscellaneous"通知通道发送通知,实际上它没有发送任何通知(仅在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())
该频道的 ID 是 fcm_fallback_notification_channel
。 firebase-messaging 库在内部创建它。
在中已经说过,Android系统默认创建的频道id是fcm_fallback_notification_channel
,但是要小心,因为系统不会创建频道直到它必须管理第一个推送通知。因此,如果您在 FirebaseMessagingService
class 的扩展中管理所有通知,则可能会发生该频道不存在并且您 运行 出现如下错误:
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);
}
问题描述
当我尝试在 Android 中发送 Notification
O 时,我必须指定要发送到的 NotificationChannel
。
如果我像这样使用旧方法(不设置任何频道)NotificationCompat.Builder(this)
,Notification
将不会显示。
对于像这样 NotificationCompat.Builder(this, "invalid")
或 NotificationCompat.Builder(this, "")
的无效频道也是如此。
当我通过 Firebase Cloud Messaging 发送通知并且我的应用程序在后台运行且没有指定 通知渠道 时,它将是"Miscellaneous" 频道中的通知。
当我尝试在上面提到的前台做同样的事情时,将不起作用,也不会创建名称为 "Miscellaneous" 和 ID [=52 的通知渠道=]"{package}.MISCELLANEOUS" 然后通过它发送。当我这样做时,会发生以下情况:
我想知道的
如何在没有像 FCM 那样的渠道的情况下发送通知,所以它落在常规 "Miscellaneous"频道?
这个工作的例子
正如我上面提到的,FCM 通知 会发生这种情况,但是例如Gmail 也使用杂项渠道。 那么如何使用呢?
我相信他们会删除杂项频道,如果它通常无法使用的话。
更简短的描述
为什么这段代码没有向"Miscellaneous"通知通道发送通知,实际上它没有发送任何通知(仅在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())
该频道的 ID 是 fcm_fallback_notification_channel
。 firebase-messaging 库在内部创建它。
在fcm_fallback_notification_channel
,但是要小心,因为系统不会创建频道直到它必须管理第一个推送通知。因此,如果您在 FirebaseMessagingService
class 的扩展中管理所有通知,则可能会发生该频道不存在并且您 运行 出现如下错误:
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);
}