简体   繁体   中英

Vibration not working on notification with custom sound

The following method shows notification with custom sound and on channel. The issue is that the vibration isn't working. I've tried researching and trying but wasn't successful.

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());
    }

It's not about custom sound. You are not setting any pattern really. The first value in vibration pattern defines the number of milliseconds to wait before turning the vibrator on . The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off and subsequent values alternate between these two. So in fact pattern sequence means OFF, ON, OFF, ON... , therefore to have any vibration, you need at least two values. I assume you most likely meant this:

notificationChannel.setVibrationPattern(new long[]{ 0, 100 });

Also, calling notificationChannel.enableVibration(true); is redundant as setting valid pattern would automatically enable it (see source ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM