Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here is the code:

int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    channel.setVibrationPattern(new long[]{ 200,200,200,200 });
    channel.enableVibration(true);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
                        .setSmallIcon(R.mipmap.icon)
                        .setContentTitle("example")
                        .setContentText("example")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());

                
                int min = 1;
                int max = 1000;
                double notificationIdD = Math.random()*(max - min + 1)+min;
                int notificationId = (int) notificationIdD;
                notificationManager.notify(notificationId, builder.build());

I already added VIBRATE uses-permission in AndroidManifest.xml so I have no idea on what to do. I also checked other similar questions but no solution work but the one that suggests the following hack:

Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);

However, this hack doesn't work when the app is in background and I need the phone to vibrate also in that case. Any help will be appreciated! :)

question from:https://stackoverflow.com/questions/65923725/notificationcompat-builder-setvibrate-is-not-vibrating-at-all-on-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...