Home » Android » Vibrate an Android Wear device on notification

Vibrate an Android Wear device on notification

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to develop an app for android and android wear. I create a notification from the mobile to the watch.

notification = new NotificationCompat.Builder(context)
                    .setSmallIcon(icon)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000, 1000})
                    .setContentTitle(title)
                    .setContentText(content)
                    .setStyle(bigStyle.bigText(content))
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), background))
                    .build();

In the manifest of the watch I set :

<uses-permission android:name="android.permission.VIBRATE"/>

Unfortunately, I haven’t any vibration.

How can I do ?

Thanks.

How to&Answers:
 .setDefaults(Notification.DEFAULT_ALL)

This line of code will make the wear wake up and vibrate.

Answer:

I would like to add 1 more updated answer for SDK 26 and above. I spent many hours trying to determine why my notifications would not vibrate on the Android Wear watch.

I eventually noticed that someone had set the priority on the notification channel to IMPORTANCE_MIN.

if (Build.VERSION.SDK_INT >= 26) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationChannel notificationChannel = new NotificationChannel(Config.NOTIFICATION_CHANNEL_ID, Config.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_MIN);
    notificationManager.createNotificationChannel(notificationChannel);
}

To fix the issue, I did the following:

  • Uninstalled the app from the phone
  • Changed the priority to HIGH [Though I suspect NORMAL will work also]
  • Reinstalled the app

Updated Code:

NotificationChannel(Config.NOTIFICATION_CHANNEL_ID, Config.NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

Note: You also need to ensure that your defaults are correct as ‘Android enthusiast’ pointed out in his earlier answer.

I hope it saves someone from an investigation.