Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vibrate on push notification

First of all I've checked all those links:

  • How to Call Vibrator inside a Service in android
  • Android notifications and vibrate from service
  • Android GCM turn on Lights

But I can't achieve my phone vibrating when I receive a push notification. Here goes my code:

PushReceiver

public class PushReceiver extends FirebaseMessagingService {
    public PushReceiver() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if(remoteMessage.getData() != null){
            Map<String, String> data = remoteMessage.getData();
            sendNotification(data.get("message"));
        }
        else{
            if(remoteMessage.getNotification() != null) {
                sendNotification(remoteMessage.getNotification().getBody());
            }
        }
    }

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, BaseActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_done_all_24dp)
                .setContentTitle(getString(R.string.str_notification_order_ready))
                .setContentText(messageBody)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        notificationBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        notificationManager.notify(ConstantUtils.NOTIFICATION_ID_ORDER_READY, notificationBuilder.build());
    }
}

Permission

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

Testing

Device: Nexus 5

Android version: 6.0.1

There is some kind of unknown sorcery that I should do to make it work?

like image 206
guisantogui Avatar asked Aug 26 '16 21:08

guisantogui


People also ask

Why doesn't my phone vibrate when I get a notification?

Launch the Settings. Locate and tap Accessibility. Scroll down a bit and tap Vibration & haptic strength. From there, enable Ring vibration, Notification vibration, and Touch feedback.

How do I get my iPhone notifications to vibrate?

Go to Settings > Sounds & Haptics or Settings > Sounds. Select an option (like Ringtone or New Mail) under Sounds and Haptic Patterns or Sounds and Vibration Patterns.


1 Answers

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}); 
like image 178
pRaNaY Avatar answered Sep 19 '22 13:09

pRaNaY