guisantogui
guisantogui

Reputation: 4126

Vibrate on push notification

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?

Upvotes: 7

Views: 15222

Answers (2)

pRaNaY
pRaNaY

Reputation: 25312

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}); 

Upvotes: 16

Joao Sardinha
Joao Sardinha

Reputation: 3423

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

Upvotes: 1

Related Questions