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

创建并配置NotificationManager,检查权限,从Preferences读取频道ID和名称。若频道ID为空,则生成新的ID和NAME。根据用户设置,动态调整通知的振动和声音效果。创建NotificationChannel,设置描述、灯光、震动模式和声音。最后,构建并发送Notification,包含跳转Intent。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//创建通知管理类
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
checkNofificationPermission(manager);
String CHANNEL_ID = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_ID, "");
String CHANNEL_NAME = PreferenceUtils.getPrefString(this, PreferenceConstants.CHANNEL_NAME, "");
int importance = NotificationManager.IMPORTANCE_DEFAULT;

boolean isVibrate = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VIBRATE_NOTIFY, true);
boolean isVoice = PreferenceUtils.getPrefBoolean(App.getInstance(), PreferenceConstants.VOICE_NOTIFY, true);
//解决通知声音、震动无法关闭或开启的问题
if (TextUtils.isEmpty(CHANNEL_ID)) {
    CHANNEL_ID = "kuda_channel" + new Random().nextInt(100000);
    PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_ID, CHANNEL_ID);
    CHANNEL_NAME = getString(R.string.app_name) + new Random().nextInt(100000);
    PreferenceUtils.setPrefString(this, PreferenceConstants.CHANNEL_NAME, CHANNEL_NAME);
}

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance);
// 配置通知渠道的属性
channel.setDescription("this is a nofifaction description !");
// 设置通知出现时的闪灯(如果 android 设备支持的话)
channel.enableLights(true);
channel.setLightColor(Color.BLUE);
if (isVibrate) {
    // 设置通知出现时的震动(如果 android 设备支持的话)
    channel.enableVibration(true);
    //如上设置使手机:静止1秒,震动2秒,静止1秒,震动3秒
    channel.setVibrationPattern(new long[]{1000, 500, 2000});
} else {
    // 设置通知出现时不震动
    channel.enableVibration(false);
    channel.setVibrationPattern(new long[]{0});
}

if (isVoice) {
    channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), null);
} else {
    channel.setSound(null, null);
}
//把渠道添加到通知中
manager.createNotificationChannel(channel);

//设置跳转的页面
PendingIntent intent = PendingIntent.getActivity(this,
        100, new Intent(this, MainActivity.class),
        PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle(title)
        .setContentText(message)
        .setContentIntent(intent)
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.mipmap.ic_cds_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_cds_launcher))
        .setLights(Color.BLUE, 2000, 1000)
        .setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    builder.setVisibility(Notification.VISIBILITY_PUBLIC);
    // 关联PendingIntent
    builder.setFullScreenIntent(intent, false);// 横幅
}

Notification notification = builder.build();
manager.notify(2, notification);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值