NotificationCompat.Builder setVibrate 在 Android 上根本不振动

问题描述

代码如下:

int importance = notificationmanager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name,importance);
    channel.setDescription(description);
    channel.setVibrationPattern(new long[]{ 200,200,200 });
    channel.enableVibration(true);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    notificationmanager notificationmanager = getSystemService(notificationmanager.class);
    notificationmanager.createNotificationChannel(channel);

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID)
                        .setSmallIcon(R.mipmap.icon)
                        .setContentTitle("example")
                        .setContentText("example")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                notificationmanagerCompat notificationmanager = notificationmanagerCompat.from(getApplicationContext());

                
                int min = 1;
                int max = 1000;
                double notificationIdD = Math.random()*(max - min + 1)+min;
                int notificationId = (int) notificationIdD;
                notificationmanager.notify(notificationId,builder.build());

我已经在 AndroidManifest.xml 中添加了 VIBRATE 使用权限,所以我不知道该怎么做。我还检查了其他类似的问题,但没有解决方案,但建议以下黑客:

Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);

但是,当应用程序处于后台时,此 hack 不起作用,并且在这种情况下我也需要手机振动。任何帮助将不胜感激! :)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)