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

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

notifications - NotificationCompat.Builder setVibrate is not vibrating at all on Android

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...