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

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

1.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不同。


上一篇:windows10离线安装docker并使用linux子系统部署nacos作为注册中心(上)


下一篇:Windows 10 1607(周年更新)后加入域无法使用WIndows Hello解决方法