无法在 Android 8 中禁用通知振动

2023-12-21

我试着禁用振动显示通知时。

Func:

public static Notification buildNotifForUploaderService(Context ctx, String title, String message) {

        Notification notification;
        NotificationCompat.Builder notificationBuilder;

        //If device is Android 8+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
            //setting pattern to disable vibrating
            notificationChannel.setVibrationPattern(new long[]{0L});
            notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
        } else {
            notificationBuilder = new NotificationCompat.Builder(ctx);
            notificationBuilder.setVibrate(new long[]{0L});
        }


        notificationBuilder
                .setContentTitle(title)
                .setContentText(message)
                .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_backup_black_24dp);


        notification = notificationBuilder.build();

        return notification;
    }

我在活动的 onCreate() 上调用它,如下所示:

Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

It is 仍在振动。我测试安卓8设备。 我也尝试过

 notificationChannel.setVibrationPattern(null);

仍然不起作用。

I have

<uses-permission android:name="android.permission.VIBRATE" />

无论我如何定义振动模式, like:

new long[]{1000L, 500L, 300L, 1000L};

振动与我的设置不符。仅发生默认的“两短”振动。

如果可以的话请帮忙,提前致谢。

E D I T:

正如 Avijit Karmakar 提到的,我已经添加了

  notificationChannel.enableVibration(false);

完整代码 now:

public class MainActivity extends AppCompatActivity {

    final static String CHANNEL_ID = "MY_CHANNEL_ID";
    final static String CHANNEL_NAME = "MY_CHANNEL_NAME";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Notification notification;
    NotificationCompat.Builder mBuilder;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        //Disabling vibration!
        notificationChannel.enableVibration(false);
        notificationManager.createNotificationChannel(notificationChannel);
        mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);

    } else {
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setVibrate(new long[]{0L});
    }

    mBuilder.setContentTitle("title")
            .setContentText("message")
            .setSmallIcon(R.drawable.ic_android_black_24dp);

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    mBuilder.setLargeIcon(bm);

    notification = mBuilder.build();
    notificationManager.notify(1, notification);
    }
}

它仍然在振动。

我在小米A1(Android 8.0)上测试

有人可以尝试这段代码并帮助我得到结果吗?


像这样answer https://stackoverflow.com/a/47646166/6031927, do:

mNotificationChannel.setVibrationPattern(new long[]{ 0 }); 
mNotificationChannel.enableVibration(true);

重要1:即使我设置了上面的振动模式,但将enableVibration设置为false,它也会振动。因此,将enableVibration设置为true!

重要2: 像这样另一个答案 https://stackoverflow.com/a/51289889/6031927,通道保持其初始设置,因此卸载并再次安装应用程序以应用更改!

希望能帮助到你!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 Android 8 中禁用通知振动 的相关文章

随机推荐