振动不适用于带有自定义声音的通知
Vibration not working on notification with custom sound
以下方法显示带有自定义声音和频道的通知。问题是振动不起作用。我尝试过研究和尝试,但没有成功。
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());
}
这与自定义声音无关。你真的没有设置任何模式。振动模式中的第一个值定义了打开振动器之前等待的毫秒数。下一个值指示振动器在关闭之前保持打开状态的毫秒数,后续值在这两个值之间交替。所以实际上模式序列意味着 OFF, ON, OFF, ON...
,因此要有任何振动,你至少需要两个值。我假设您很可能是这个意思:
notificationChannel.setVibrationPattern(new long[]{ 0, 100 });
此外,调用 notificationChannel.enableVibration(true);
是多余的,因为设置有效模式会自动启用它(参见 source)。
以下方法显示带有自定义声音和频道的通知。问题是振动不起作用。我尝试过研究和尝试,但没有成功。
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());
}
这与自定义声音无关。你真的没有设置任何模式。振动模式中的第一个值定义了打开振动器之前等待的毫秒数。下一个值指示振动器在关闭之前保持打开状态的毫秒数,后续值在这两个值之间交替。所以实际上模式序列意味着 OFF, ON, OFF, ON...
,因此要有任何振动,你至少需要两个值。我假设您很可能是这个意思:
notificationChannel.setVibrationPattern(new long[]{ 0, 100 });
此外,调用 notificationChannel.enableVibration(true);
是多余的,因为设置有效模式会自动启用它(参见 source)。