Android O中修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效
【摘要】
ndroid 8.0中修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效,原代码如下:
public void notifyDownloading(long progress, long num, String file_name) { Notification.Build...
ndroid 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不同。
文章来源: chengsy.blog.csdn.net,作者:程思扬,版权归原作者所有,如需转载,请联系作者。
原文链接:chengsy.blog.csdn.net/article/details/80449108
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)