7

First of all I've checked all those links:

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?

Community
  • 1
  • 1
guisantogui
  • 4,017
  • 9
  • 49
  • 93
  • You may want to check this SO [solution](http://stackoverflow.com/a/36869888/5995040) or [this](http://stackoverflow.com/a/33951000/5995040). This might help you solve your issue. – Mr.Rebot Aug 27 '16 at 15:25
  • @Mr.Rebot I'm sorry for the delay to reply, but none of that solutions worked for me! :( – guisantogui Sep 04 '16 at 15:02

2 Answers2

16

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}); 
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • `setPriority()` works fine for sound and vibrate on devices which have `Oreo` but It wasn't working for `Pre-Oreo` ones. Using `setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)` finally worked fine! – Wahib Ul Haq Jun 18 '18 at 00:49
1

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

John Sardinha
  • 3,566
  • 6
  • 25
  • 55