Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vibrate Not working with Notification

I am trying to set vibrate and sound for notification. For some reason its not working for me :( Here is the code what I am trying

NotificationManager notificationManager = getNotificationManager();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                context);
        builder.setSound(alarmSound);
        builder.setVibrate(new long[] { 1000, 1000, 1000 });
        Notification notification = builder.setContentIntent(contentIntent)
                .setSmallIcon(icon).setTicker(title).setWhen(0)
                .setAutoCancel(true).setContentTitle(title).setPriority(Notification.PRIORITY_HIGH)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msgToDisply))
                .setContentText(msgToDisply).build();
        notificationManager.notify(NOTIFICATION, notification);
        stopSelf();

And

public NotificationManager getNotificationManager() {
        return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

And I have permission in my manifest

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

Any clue what's going on ?

like image 418
Achayan Avatar asked Dec 28 '14 22:12

Achayan


People also ask

Why do my notifications not vibrate anymore?

Enable Vibration in Accessibility Besides Sounds & Haptics, another place you need to check is your Accessibility settings. If vibration isn't enabled here, your iPhone will not vibrate on Silent or Ring mode either. Therefore, go to Settings > Accessibility > Touch, and then toggle on Vibration.


2 Answers

add this

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
like image 106
Panayiotis Irakleous Avatar answered Oct 19 '22 21:10

Panayiotis Irakleous


It is because you did not declare the Vibrator class to vibrate on Notification.In your notification builder put this code and set the duration of vibrate based on your choose.

    Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);
like image 3
BiggDawgg Avatar answered Oct 19 '22 21:10

BiggDawgg