我已经查看过有关此主题的几个问题,并且似乎都建议在通知构建器中简单地使用.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);
}
你能发现我做错的事吗?我找了几个小时,却找不到任何东西。
答案 0 :(得分:0)
问题是您正在使用这行代码修改默认通知行为(声音和振动)。
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());
我希望这对你有所帮助! ;)