java - Android:通知不生成默认声音
问题描述
我正在尝试用声音生成 FCM 通知。我收到通知等没有问题,但根本没有声音。我可以接受默认的通知声音。请检查以下代码。它适用于 API 26 及更高版本。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "xxx";
// The user-visible name of the channel.
CharSequence name = "xxx";
// The user-visible description of the channel.
String description = "xxx";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String CHANNEL_ID = "xxx";
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(this,"xxx")
.setSmallIcon(R.drawable.volusha_notifications)
.setContentText(text)
.setChannelId(CHANNEL_ID)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setAutoCancel(true)
.build();
// Issue the notification.
mNotificationManager.notify(new Random().nextInt(), notification);
为什么会发生这种情况以及如何获得默认声音?
解决方案
更改此部分:
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(this,"xxx")
.setSmallIcon(R.drawable.volusha_notifications)
.setContentText(text)
.setChannelId(CHANNEL_ID)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setAutoCancel(true)
.build();
至
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(this,"xxx")
.setSmallIcon(R.drawable.volusha_notifications)
.setContentText(text)
.setChannelId(CHANNEL_ID)
.setContentIntent(pendingIntent)
.setContentTitle(title)
.setSound(RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.build();
推荐阅读
- r - 查找每个向量元素所在的范围
- flask - flask开发单终端:不退出flask继续编辑代码
- mysql - 无法将文件加载到 mysql 表:错误代码:2 - 没有这样的文件或目录
- php - 在我的 mysql 表中,在一个单元格中有行,我有数组。如何选择具有特定数组值的行?
- gams-math - gams 指定具有大量零的通量网络(稀疏矩阵)
- java - 在字符串数组中查找字符串值的搜索方法
- javascript - Slack bot 私人频道发布消息
- docker - 为什么在运行 docker 容器后网站会打印 403 禁止的 nginx 错误?
- rest - REST API:将“批量”附加到 api 以在同一资源上创建批量创建?
- rpm - 如何删除从 rpm 安装后安装但升级时不需要的文件?