适配安卓12PendingIntent

pull/27/head
Moredistant 3 years ago
parent 08e5c8fbb6
commit 15eddd9cfb
  1. 37
      app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.java

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

Loading…
Cancel
Save