我尝试在收到通知时设置默认的震动和声音提醒,但目前为止没有成功。我想这可能与我设置默认值的方式有关,但我不确定如何修复它。有什么想法吗?
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);
}
9条答案
按热度按时间f0ofjuux1#
一些伪代码可能会对您有所帮助。
在
AndroidManifest.xml
文件中添加以下振动权限esyap4oy2#
TeeTracker答案的延伸,
要获得默认通知声音,您可以执行以下操作
这将为您提供默认的通知声音。
lztngnrs3#
通知****振动
"声音"
for more sound option
xriantvc4#
它的工作对我来说很好,你可以试试。
46scxncf5#
这是一个简单的方法调用通知使用默认振动和声音从系统.
如果您要使用振动权限,请添加该权限:
祝你好运。
yqhsw0fo6#
我正在使用下面的代码,它对我来说工作得很好。
af7jpaap7#
要支持SDK版本〉= 26,您还应该构建NotificationChanel并在其中设置振动模式和声音。
这是建造者
请确保您的
AudioAttributes
选择正确,以阅读更多的here。yzuktlbb8#
//设置通知音频
9udxz4iz9#
为了Kotlin你可以试试这个.