Android(安卓)O中修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效
16lz
2021-01-26
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不同。
更多相关文章
- 【Android】AppCompat V21:将 Materia Design 兼容到5.0之前的设
- Android(安卓)Studio同一工程生成不同的app名,包名,图标分别设置
- android:priority
- android 5 设置图案锁,锁屏界面进入相机拍照、查看锁定的照片后,退
- Android重写View并且自定义属性(二)
- 微信视频全屏问题解决方案
- Android系统的开机画面显示过程分析(8)
- Android调用第三方百度APP进行导航
- Android-EditText属性大全