Vibrate Not working with Notification

送分小仙女□ 提交于 2019-12-13 14:52:20

问题


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 ?


回答1:


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



回答2:


add this

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;


来源:https://stackoverflow.com/questions/27681342/vibrate-not-working-with-notification

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