我在这里尝试过很多东西,但没有什么对我有用。 这是我的源代码:
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContentText("Hello World!");
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
通知出现但没有声音,有什么帮助吗?
答案 0 :(得分:20)
设置默认标志:
mBuilder.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS|Notification.DEFAULT_VIBRATE);
答案 1 :(得分:5)
使用 notification.sound 代替mBuilder.setSound()解决了我的问题。
这是代码 -
Notification notification = mBuilder.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager.notify(0, notification);
答案 2 :(得分:2)
我在设置声音的方式上看不出任何错误。另一种替代方法是以下列方式设置声音。试一试,看看它是否有帮助...
Uri alertSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setContentText("Hello World!")
.setSound(alertSound);
<强>更新强>
请尝试按如下方式编辑NotificationManager
并尝试...
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.getNotification();
notification.flags = Notification.FLAG_AUTO_CANCEL; // cancel the notification when clicked by the user
mNotificationManager.notify(0, notification);
答案 3 :(得分:0)
检查我的声音设置。通知水平一直降到零。
这很棘手,因为除了通知之外,其他声音都能正常工作。
答案 4 :(得分:0)
这也可能是由于与较旧的API版本的轻微不兼容。例如,在SDK 25上进行编译但是针对旧版本(例如SDK 15)时,我添加了以下行来解决问题:
notification.audioStreamType = AudioManager.STREAM_ALARM;
答案 5 :(得分:-1)
只改变这个:
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
到
Uri _nSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(_nSound);