振动不适用于通知

Ach*_*yan 2 android android-notifications android-vibration

我试图设置振动和声音通知.由于某种原因它不适合我:(这是我正在尝试的代码

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();
Run Code Online (Sandbox Code Playgroud)

public NotificationManager getNotificationManager() {
        return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
Run Code Online (Sandbox Code Playgroud)

我的证词中有我的许可

<uses-permission android:name="android.permission.VIBRATE" />
Run Code Online (Sandbox Code Playgroud)

有什么想法发生了什么?

Pan*_*ous 5

加上这个

notification.defaults|= Notification.DEFAULT_SOUND;
notification.defaults|= Notification.DEFAULT_LIGHTS;
notification.defaults|= Notification.DEFAULT_VIBRATE;
Run Code Online (Sandbox Code Playgroud)


小智 3

这是因为您没有声明 Vibrator 类在通知时振动。在通知生成器中放置此代码并根据您的选择设置振动持续时间。

    Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
 // Vibrate for 500 milliseconds
 v.vibrate(500);
Run Code Online (Sandbox Code Playgroud)

  • 这是一个黑客,不是回答问题 (4认同)
  • 我想知道与@StealthRabbi同样的事情,上面的建议会起作用,但它只是手动触发振动,我本以为NotificationBuilders.setVibrate()方法应该处理这个! (3认同)