8.0之后NotificationManager创建之后需要添加NotificationChannel
NotificationManager mNotificationManager;
//mNotificationId 设置成员变量方便取消时候使用NotificationManager.cancel(mNotificationId);
mNotificationId = (int) System.currentTimeMillis();
mChannelId = fileName + mNotificationId;//尽量使用唯一值
private void initNotification(String fileName) {
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel
(mChannel, "通知标题", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(false);//震动不可用
channel.setSound(null, null); //设置没有声音
mNotificationManager.createNotificationChannel(channel);
}
}
mBuilder = new NotificationCompat.Builder(this, mChannel).setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentInfo("下载中...");
mNotification = mBuilder.build();
mNotificationManager.notify(mNotificationId, mNotification);
}
//设置通知栏下载进度
private void setNotificationProgress(int progress) {
mBuilder.setProgress(100, progress, false);
mNotification = mBuilder.build();
mNotificationManager.notify(mNotificationId, mNotification);
}
mNotificationManager.cancel(mNotificationId); //合适的时候取消 或者 添加pendingIntent设置点击事件自动取消等