【发布时间】:2013-08-17 16:32:46
【问题描述】:
我正在尝试在收到通知时获得默认的振动和声音警报,但到目前为止还没有成功。我想这与我设置默认值的方式有关,但我不确定如何修复它。有什么想法吗?
public void connectedNotify() {
Integer mId = 0;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentTitle("Device Connected")
.setContentText("Click to monitor");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setOngoing(true);
Notification note = mBuilder.build();
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_SOUND;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, note);
}
【问题讨论】:
-
所有答案都只是重申了您已经拥有的代码的一些变体,在我看来,他们都没有回答这个问题。你的代码看起来不错,AFAICT。很可能,您只是在 AndroidManifest.xml 中缺少
android.permission.VIBRATE。 -
谁可以在此线程中应用解决方案,但通知仍然没有任何振动,您可能需要先启用通知通道的振动。看看这个:stackoverflow.com/a/47646166/8551764
标签: android