【发布时间】:2019-05-10 02:53:40
【问题描述】:
使用Android Developer docs 中的以下代码,我无法在 API 27 (Android O) 模拟器中获得声音。它适用于 API 24 设备。我还仔细检查了通知设置,通知通道设置为播放默认声音。
这是一个包含以下示例代码的项目,您可以在模拟器上试用:Github。
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "test-channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel newIncidentChannel = new NotificationChannel(channelId,
"Test Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(newIncidentChannel);
}
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Test")
.setContentText("Text")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
notificationManager.notify("test", NOTIFICATION_ID, notificationBuilder.build());
2018 年 5 月 16 日更新:
我在这里使用解决方案:https://stackoverflow.com/a/46862503/817886 在收到通知时使用媒体播放来播放声音。不理想,但在找到合适的解决方案之前使用它。
2018 年 5 月 29 日更新:
最新版本的 Android 8.1.0 已修复此问题。
【问题讨论】:
-
我遇到了同样的问题,并且阅读了几乎所有关于这个声音问题的答案。终于听到提示音了。问题是,如果您订阅了任何频道并且修改/编辑此频道,则通知声音不起作用。只需删除已安装的应用程序并重新安装。