Vibrate on push notification

不打扰是莪最后的温柔 提交于 2019-12-04 04:21:50

You can also use setDefaults (int defaults) to your NotificationCompat.Builder instance which provides you to default system sound, vibrate and lights for your notification.

The value should be one or more of the following fields combined with bitwise-or(|): DEFAULT_SOUND, DEFAULT_VIBRATE, DEFAULT_LIGHTS.

For all default values, use DEFAULT_ALL.

Ex. As per your code you are setting default sound so, if you want to set default sound and vibrate:

notificationBuilder.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE);

If you want all default setting , you can achieve it by setting notificationBuilder.setDefaults(-1) , it consider as DEFAULT_ALL value.

See android doc for setDefaults.

EDIT:

The vibration has a delay of 1000 ms. If you set the first one to 0, it will go off instantly. It's a { delay, vibrate, sleep, vibrate, sleep } pattern

 // Each element then alternates between delay, vibrate, sleep, vibrate, sleep
 notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000}); 

This vibrates the phone:

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

When you call the notification also call this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!