|
|
|
@ -29,6 +29,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示开始下载时的通知 |
|
|
|
|
* |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param channelName |
|
|
|
@ -53,7 +54,11 @@ public class NotificationUtils { |
|
|
|
|
if (isCancelDownload) { |
|
|
|
|
Intent intent = new Intent(context, DownloadService.class); |
|
|
|
|
intent.putExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE, true); |
|
|
|
|
PendingIntent deleteIntent = PendingIntent.getService(context, notifyId,intent, PendingIntent.FLAG_CANCEL_CURRENT); |
|
|
|
|
int flag = PendingIntent.FLAG_CANCEL_CURRENT; |
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
|
flag = flag | PendingIntent.FLAG_IMMUTABLE; |
|
|
|
|
} |
|
|
|
|
PendingIntent deleteIntent = PendingIntent.getService(context, notifyId, intent, flag); |
|
|
|
|
builder.setDeleteIntent(deleteIntent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -69,6 +74,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示下载中的通知(更新进度) |
|
|
|
|
* |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
@ -83,7 +89,11 @@ public class NotificationUtils { |
|
|
|
|
if (isCancelDownload) { |
|
|
|
|
Intent intent = new Intent(context, DownloadService.class); |
|
|
|
|
intent.putExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE, true); |
|
|
|
|
PendingIntent deleteIntent = PendingIntent.getService(context, notifyId,intent, PendingIntent.FLAG_CANCEL_CURRENT); |
|
|
|
|
int flag = PendingIntent.FLAG_CANCEL_CURRENT; |
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
|
flag = flag | PendingIntent.FLAG_IMMUTABLE; |
|
|
|
|
} |
|
|
|
|
PendingIntent deleteIntent = PendingIntent.getService(context, notifyId, intent, flag); |
|
|
|
|
builder.setDeleteIntent(deleteIntent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -100,6 +110,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示下载完成时的通知(点击安装) |
|
|
|
|
* |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
@ -112,7 +123,11 @@ public class NotificationUtils { |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(context, channelId, icon, title, content); |
|
|
|
|
builder.setAutoCancel(true); |
|
|
|
|
Intent intent = AppUtils.getInstallIntent(context, file, authority); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getActivity(context, notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
int flag = PendingIntent.FLAG_UPDATE_CURRENT; |
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
|
flag = flag | PendingIntent.FLAG_IMMUTABLE; |
|
|
|
|
} |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getActivity(context, notifyId, intent, flag); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; |
|
|
|
@ -121,6 +136,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 现在下载失败通知 |
|
|
|
|
* |
|
|
|
|
* @param context |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
@ -133,14 +149,18 @@ public class NotificationUtils { |
|
|
|
|
public static void showErrorNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, boolean isReDownload, UpdateConfig config) { |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(context, channelId, icon, title, content); |
|
|
|
|
builder.setAutoCancel(true); |
|
|
|
|
int flag = PendingIntent.FLAG_UPDATE_CURRENT; |
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
|
|
|
|
flag = flag | PendingIntent.FLAG_IMMUTABLE; |
|
|
|
|
} |
|
|
|
|
if (isReDownload) {//重新下载
|
|
|
|
|
Intent intent = new Intent(context, DownloadService.class); |
|
|
|
|
intent.putExtra(Constants.KEY_RE_DOWNLOAD, true); |
|
|
|
|
intent.putExtra(Constants.KEY_UPDATE_CONFIG, config); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(context, notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(context, notifyId, intent, flag); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
} else { |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(context, notifyId,new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(context, notifyId, new Intent(), flag); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -152,6 +172,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示通知信息(非第一次) |
|
|
|
|
* |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
@ -168,6 +189,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 取消通知 |
|
|
|
|
* |
|
|
|
|
* @param notifyId |
|
|
|
|
*/ |
|
|
|
|
public static void cancelNotification(Context context, int notifyId) { |
|
|
|
@ -177,6 +199,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取通知管理器 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static NotificationManager getNotificationManager(Context context) { |
|
|
|
@ -185,6 +208,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建一个通知渠道(兼容0以上版本) |
|
|
|
|
* |
|
|
|
|
* @param channelId |
|
|
|
|
* @param channelName |
|
|
|
|
*/ |
|
|
|
@ -201,6 +225,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建一个通知构建器 |
|
|
|
|
* |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
@ -213,6 +238,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建一个通知构建器 |
|
|
|
|
* |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
@ -238,6 +264,7 @@ public class NotificationUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新通知栏 |
|
|
|
|
* |
|
|
|
|
* @param id |
|
|
|
|
* @param notification |
|
|
|
|
*/ |
|
|
|
|