0

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! :)

Frank
  • 69
  • 8
  • for Oreo and above you have to enable vibration in the Notification channel. check out [this](https://stackoverflow.com/a/47646166/9701793), while creating a channel enable vibration. – rahat Jan 27 '21 at 17:47
  • I updated the question with the working version of the code, with correct values in the long[] array. Thanks! :) – Frank Jan 29 '21 at 09:08

0 Answers0