Android O中修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效

Android 8.0中修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效,原代码如下:

public void notifyDownloading(long progress, long num, String file_name) {
    Notification.Builder mBuilder;
    mBuilder = new Notification.Builder(MainActivity.this, TAG );
    NotificationChannel channel;
    channel = new NotificationChannel(TAG , file_name, NotificationManager.IMPORTANCE_LOW);
    mNotifyManager.createNotificationChannel(channel);
    mBuilder.setSmallIcon(R.drawable.notification_download_icon);
    mBuilder.setProgress((int) num, (int) progress, false);
    mBuilder.setContentInfo(getPercent((int) progress, (int) num));
    mBuilder.setOngoing(true);
    mBuilder.setWhen(System.currentTimeMillis());
    mBuilder.setContentTitle(file_name);
    mBuilder.setContentText("download");
    PendingIntent pendIntent = PendingIntent.getActivity(
            MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendIntent);
    mNotifyManager.notify(NOTIFY_ID, mBuilder.build());
}

这里将IMPORTANCE_HIGH修改为IMPORTANCE_LOW,通过Android Studio直接安装,发现修改不生效,app的效果还是IMPORTANCE_HIGH属性的效果。总之,一脸懵逼。。。

经过若干猜测和尝试,发现修改每次创建Notification.Builder的id和NotificationChannel的id就可以规避该问题,修改后代码如下:

public void notifyDownloading(long progress, long num, String file_name) {
    Notification.Builder mBuilder;
    mBuilder = new Notification.Builder(MainActivity.this, TAG + System.currentTimeMillis());
    NotificationChannel channel;
    channel = new NotificationChannel(TAG + System.currentTimeMillis(), file_name, NotificationManager.IMPORTANCE_LOW);
    mNotifyManager.createNotificationChannel(channel);
    mBuilder.setSmallIcon(R.drawable.notification_download_icon);
    mBuilder.setProgress((int) num, (int) progress, false);
    mBuilder.setContentInfo(getPercent((int) progress, (int) num));
    mBuilder.setOngoing(true);
    mBuilder.setWhen(System.currentTimeMillis());
    mBuilder.setContentTitle(file_name);
    mBuilder.setContentText("download");
    PendingIntent pendIntent = PendingIntent.getActivity(
            MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendIntent);
    mNotifyManager.notify(NOTIFY_ID, mBuilder.build());
}
通过System.currentTimeMillis()保证每次创建对象的Id不同。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值