我看过有关此主题的几个问题,似乎都建议在通知生成器中仅使用.setSound(alarmSound)
会使电话在通知中播放声音。但是,我只是在这样做,它是行不通的。
我可能丢失了一些东西,但找不到。
这是我用来发出通知的代码:
private void newArrivalNotification(String locId, String userId) {
...
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mainActivity = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, locId.hashCode(),
mainActivity, 0);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alarmSound == null) {
Log.w("GcmIntentService", "alarmSound is null");
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setSound(alarmSound, AudioManager.STREAM_NOTIFICATION)
.setTicker(msg)
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
Notification arrivalNotification = mBuilder.build();
arrivalNotification.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(NOTIFICATION_ID, arrivalNotification);
}
你能发现我做错了什么吗?我看了几个小时,找不到任何东西。
最佳答案
问题是您正在使用此代码行修改默认的通知行为(声音和振动)。
Notification arrivalNotification = mBuilder.build();
arrivalNotification.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(NOTIFICATION_ID, arrivalNotification);
您可以使用以下方法再次激活它:
int defaults = 0;
defaults |= Notification.DEFAULT_SOUND;
defaults |= Notification.DEFAULT_VIBRATE;
mBuilder.setDefaults(defaults);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
希望对您有所帮助! ;)
关于android - Android通知-setSound不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25613641/