Android 振动不适用于带有自定义声音的通知
以下方法通过自定义声音和频道显示通知。问题是振动不起作用。我试过研究和尝试,但没有成功Android 振动不适用于带有自定义声音的通知,android,android-10.0,Android,Android 10.0,以下方法通过自定义声音和频道显示通知。问题是振动不起作用。我试过研究和尝试,但没有成功 private void showNotification(){ Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.ge
private void showNotification(){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.messenger);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, ChannelId)
.setSmallIcon(R.drawable.ic_stat_facebookmessengernotificationicon)
.setContentTitle("John Doe")
.setContentText("Hey, What's Up!")
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
if(soundUri != null){
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION)
.build();
NotificationChannel notificationChannel = new NotificationChannel(ChannelId,"Messenger",NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setSound(soundUri,audioAttributes);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{ 100 });
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
mNotificationManager.notify(0, notificationBuilder.build());
}
这与定制声音无关。你真的没有设定任何模式。振动模式中的第一个值定义打开可控震源之前等待的毫秒数。下一个值指示在关闭可控震源之前保持可控震源打开的毫秒数,后续值在这两个值之间交替。所以事实上,模式序列意味着关闭,打开,关闭,打开…,所以要有任何振动,至少需要两个值。我想你很可能是这个意思:
notificationChannel.setVibrationPattern(new long[]{ 0, 100 });
另外,调用notificationChannel.enablesvibration(true)代码>是多余的,因为设置有效模式会自动启用它(请参阅)