【问题标题】:How to send to "Miscellaneous" notification channel?如何发送到“杂项”通知渠道?
【发布时间】:2017-07-31 13:05:48
【问题描述】:

问题描述

当我尝试在 Android O 中发送 Notification 时,我必须指定要发送到的 NotificationChannel。

如果我使用像NotificationCompat.Builder(this)这样的旧方法(不设置任何频道),Notification将不会显示。

对于像 NotificationCompat.Builder(this, "invalid") 或 NotificationCompat.Builder(this, "") 这样的无效频道也是如此。

当我通过 Firebase Cloud Messaging 发送通知并将我的应用程序置于后台且未指定 通知通道 时,它将是 中的通知其他” 频道。

当我尝试在上面提到的前台执行相同操作时,将无法创建名称为 "Miscellaneous" 和 ID "{package}.MISCELLANEOUS" 的通知通道 然后通过它发送。当我这样做时,会发生以下情况:

我想知道的

我如何在没有像 FCM 这样的渠道的情况下发送通知,使其进入常规的“杂项”渠道?

这个工作的例子

正如我上面提到的,它发生在 FCM 通知 中,但是例如Gmail 也使用其他渠道。 那我该如何使用呢?

如果杂项频道通常无法使用,我相信他们会删除它。

简短描述

为什么这段代码没有向“杂项”通知通道发送通知,实际上是没有发送任何通知(仅在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 在您的包中是唯一的。使用任何所需的初始设置(例如警报声音)以及对用户可见的可选描述来配置通知通道对象。将通知通道对象提交给通知管理器。developer.android.com/preview/features/…
  • @creativecreatorormaybenot 您找到解决方案了吗?您是否尝试过刚刚发布的最终版 Android 8(与开发预览版相比)?
  • @Arhimed 我想我没有尝试过最终的 Android 8。

标签: android android-notifications android-8.0-oreo


【解决方案1】:

【讨论】:

  • 查看 Sca09 的回答,因为并非总是如此。
【解决方案2】:

正如another answer中所说,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);
}

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多