Android通知不会发出声音或振动

int icon = R.drawable.icon; Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Countdown Complete!"; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(); Notification notification = new Notification(icon, "is completed!", System.currentTimeMillis()); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent); long[] vibrate = {0,100,200,300}; notification.vibrate = vibrate; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.defaults |= Notification.DEFAULT_SOUND; notificationManager.notify(myCountDown.getId(), notification); 

这个代码在我的Android应用程序中,我收到通知但没有声音或振动。

我已经在多部手机上进行了测试,声音和振动都在设置中打开和关闭。 我也确定我要求在android清单中使用振动许可但我仍然只收到通知……

我也尝试过:

 notification.defaults = Notification.DEFAULT_ALL; 

 notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.defaults |= Notification.DEFAULT_SOUND; 

如何在通知中获得声音和振动?

不要忘记启用铃声和通知的振动设置。 转到设置 – >声音。 检查“声音振动”。

notification.defaults | = Notification.DEFAULT_VIBRATE; 不起作用?

步骤1:确保清单中具有VIBRATE权限,并且您正在具有振动电机的设备上运行。

步骤#2:摆脱getApplicationContext() ,因为这里不需要它。

步骤3:尝试通过Notification上的sound数据成员指定实际铃声,看看是否默认不播放(你需要在flags没有DEFAULT_SOUND )。

 Notification notif ... //create your notification 

notif.defaults | = Notification.DEFAULT_SOUND; notif.defaults | = Notification.DEFAULT_VIBRATE;

不要忘记在清单中包含Vibrate权限。