Vibrate on push notification

空扰寡人 提交于 2019-12-12 08:38:47

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/39174818/vibrate-on-push-notification

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