当前位置:   article > 正文

解决Android通知优先级过低导致设置震动无反应的问题_安卓notification设置震动无效

安卓notification设置震动无效
  1. //创建通知管理类
  2. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  3. checkNofificationPermission(manager);
  4. String CHANNEL_ID = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_ID, "");
  5. String CHANNEL_NAME = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_NAME, "");
  6. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  7. boolean isVibrate = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VIBRATE_NOTIFY, true);
  8. boolean isVoice = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VOICE_NOTIFY, true);
  9. //解决通知声音、震动无法关闭或开启的问题
  10. if (TextUtils.isEmpty(CHANNEL_ID)) {
  11. CHANNEL_ID = "kuda_channel" + new Random().nextInt(100000);
  12. PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_ID, CHANNEL_ID);
  13. CHANNEL_NAME = getString(R.string.app_name) + new Random().nextInt(100000);
  14. PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_NAME, CHANNEL_NAME);
  15. }
  16. NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
  17. // 配置通知渠道的属性
  18. channel.setDescription("this is a nofifaction description !");
  19. // 设置通知出现时的闪灯(如果 android 设备支持的话)
  20. channel.enableLights(true);
  21. channel.setLightColor(Color.BLUE);
  22. if (isVibrate) {
  23. // 设置通知出现时的震动(如果 android 设备支持的话)
  24. channel.enableVibration(true);
  25. //如上设置使手机:静止1秒,震动2秒,静止1秒,震动3
  26. channel.setVibrationPattern(new long[]{1000, 500, 2000});
  27. } else {
  28. // 设置通知出现时不震动
  29. channel.enableVibration(false);
  30. channel.setVibrationPattern(new long[]{0});
  31. }
  32. if (isVoice) {
  33. channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), null);
  34. } else {
  35. channel.setSound(null, null);
  36. }
  37. //把渠道添加到通知中
  38. manager.createNotificationChannel(channel);
  39. //设置跳转的页面
  40. PendingIntent intent = PendingIntent.getActivity(this,
  41. 100, new Intent(this, MainActivity.class),
  42. PendingIntent.FLAG_UPDATE_CURRENT);
  43. NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
  44. .setContentTitle(title)
  45. .setContentText(message)
  46. .setContentIntent(intent)
  47. .setWhen(System.currentTimeMillis())
  48. .setSmallIcon(R.mipmap.ic_cds_launcher)
  49. .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_cds_launcher))
  50. .setLights(Color.BLUE, 2000, 1000)
  51. .setAutoCancel(true);
  52. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  53. builder.setVisibility(Notification.VISIBILITY_PUBLIC);
  54. // 关联PendingIntent
  55. builder.setFullScreenIntent(intent, false);// 横幅
  56. }
  57. Notification notification = builder.build();
  58. manager.notify(2, notification);
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/111455
推荐阅读
相关标签
  

闽ICP备14008679号