通知无法在 Android Oreo (API 26) 中显示

编程入门 行业动态 更新时间:2024-10-19 15:23:41
本文介绍了通知无法在 Android Oreo (API 26) 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Android O 上尝试显示通知时收到此消息.

I get this message when trying to display a notification on Android O.

不推荐将流类型用于除volume 之外的操作控制

Use of stream types is deprecated for operations other than volume control

通知直接来自示例文档,在 Android 25 上显示正常.

The notification is straight from the example docs, and displays fine on Android 25.

推荐答案

从Android O开始,你需要配置一个NotificationChannel,并在您尝试显示通知时引用该频道.

Starting with Android O, you are required to configure a NotificationChannel, and reference that channel when you attempt to display a notification.

private static final int NOTIFICATION_ID = 1; private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel"; ... NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT); // Configure the notification channel. notificationChannel.setDescription("Channel description"); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000}); notificationChannel.enableVibration(true); notificationManager.createNotificationChannel(notificationChannel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .setVibrate(new long[]{0, 100, 100, 100, 100, 100}) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Content Title") .setContentText("Content Text"); notificationManager.notify(NOTIFICATION_ID, builder.build());

几个重要的注意事项:

  • NotificationChannel 中指定的振动模式等设置会覆盖实际 Notification 中指定的设置.我知道,这是违反直觉的.您应该将要更改的设置移动到通知中,或者为每个配置使用不同的 NotificationChannel.
  • 在将NotificationChannel 设置传递给createNotificationChannel() 之后,您将无法修改大部分设置.您甚至无法调用 deleteNotificationChannel() 然后尝试重新添加它.使用已删除的 NotificationChannel 的 ID 将使其复活,并且与首次创建时一样不可变.在卸载应用程序之前,它将继续使用旧设置.因此,您最好确定您的频道设置,并在您使用这些设置时重新安装应用程序,以使它们生效.
  • Settings such as vibration pattern specified in the NotificationChannel override those specified in the actual Notification. I know, its counter-intuitive. You should either move settings that will change into the Notification, or use a different NotificationChannel for each configuration.
  • You cannot modify most of the NotificationChannel settings after you've passed it to createNotificationChannel(). You can't even call deleteNotificationChannel() and then try to re-add it. Using the ID of a deleted NotificationChannel will resurrect it, and it will be just as immutable as when it was first created. It will continue to use the old settings until the app is uninstalled. So you had better be sure about your channel settings, and reinstall the app if you are playing around with those settings in order for them to take effect.
  • 更多推荐

    通知无法在 Android Oreo (API 26) 中显示

    本文发布于:2023-11-27 08:19:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1637255.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:通知   Android   Oreo   API

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!