适配安卓12PendingIntent

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

@ -23,12 +23,13 @@ import androidx.core.app.NotificationCompat;
*/
public class NotificationUtils {
private NotificationUtils(){
private NotificationUtils() {
throw new AssertionError();
}
/**
* 显示开始下载时的通知
*
* @param notifyId
* @param channelId
* @param channelName
@ -36,39 +37,44 @@ public class NotificationUtils {
* @param title
* @param content
*/
public static void showStartNotification(Context context, int notifyId,String channelId, String channelName,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isVibrate,boolean isSound, boolean isCancelDownload){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createNotificationChannel(context,channelId,channelName,isVibrate,isSound);
public static void showStartNotification(Context context, int notifyId, String channelId, String channelName, @DrawableRes int icon, CharSequence title, CharSequence content, boolean isVibrate, boolean isSound, boolean isCancelDownload) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(context, channelId, channelName, isVibrate, isSound);
}
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
NotificationCompat.Builder builder = buildNotification(context, channelId, icon, title, content);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if(isVibrate && isSound){
if (isVibrate && isSound) {
builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
}else if(isVibrate){
} else if (isVibrate) {
builder.setDefaults(Notification.DEFAULT_VIBRATE);
}else if(isSound){
} else if (isSound) {
builder.setDefaults(Notification.DEFAULT_SOUND);
}
if(isCancelDownload){
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);
intent.putExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE, true);
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);
}
Notification notification = builder.build();
if(isCancelDownload){
if (isCancelDownload) {
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
}else{
} else {
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
}
notifyNotification(context,notifyId,notification);
notifyNotification(context, notifyId, notification);
}
/**
* 显示下载中的通知更新进度
*
* @param notifyId
* @param channelId
* @param icon
@ -77,29 +83,34 @@ public class NotificationUtils {
* @param progress
* @param size
*/
public static void showProgressNotification(Context context, int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,int progress,int size, boolean isCancelDownload){
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content,progress,size);
public static void showProgressNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, int progress, int size, boolean isCancelDownload) {
NotificationCompat.Builder builder = buildNotification(context, channelId, icon, title, content, progress, size);
if(isCancelDownload){
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);
intent.putExtra(Constants.KEY_STOP_DOWNLOAD_SERVICE, true);
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);
}
Notification notification = builder.build();
if(isCancelDownload){
if (isCancelDownload) {
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
}else{
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
} else {
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
}
notifyNotification(context,notifyId,notification);
notifyNotification(context, notifyId, notification);
}
/**
* 显示下载完成时的通知点击安装
*
* @param notifyId
* @param channelId
* @param icon
@ -107,20 +118,25 @@ public class NotificationUtils {
* @param content
* @param file
*/
public static void showFinishNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, File file, String authority){
cancelNotification(context,notifyId);
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
public static void showFinishNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, File file, String authority) {
cancelNotification(context, notifyId);
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);
Intent intent = AppUtils.getInstallIntent(context, file, authority);
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;
notifyNotification(context,notifyId,notification);
notifyNotification(context, notifyId, notification);
}
/**
* 现在下载失败通知
*
* @param context
* @param notifyId
* @param channelId
@ -130,70 +146,78 @@ public class NotificationUtils {
* @param isReDownload
* @param 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);
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);
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);
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, flag);
builder.setContentIntent(clickIntent);
}else{
PendingIntent clickIntent = PendingIntent.getService(context, notifyId,new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
} else {
PendingIntent clickIntent = PendingIntent.getService(context, notifyId, new Intent(), flag);
builder.setContentIntent(clickIntent);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notifyNotification(context,notifyId,notification);
notifyNotification(context, notifyId, notification);
}
/**
* 显示通知信息非第一次
*
* @param notifyId
* @param channelId
* @param icon
* @param title
* @param content
*/
public static void showNotification(Context context, int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isAutoCancel){
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
public static void showNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, boolean isAutoCancel) {
NotificationCompat.Builder builder = buildNotification(context, channelId, icon, title, content);
builder.setAutoCancel(isAutoCancel);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notifyNotification(context,notifyId,notification);
notifyNotification(context, notifyId, notification);
}
/**
* 取消通知
*
* @param notifyId
*/
public static void cancelNotification(Context context, int notifyId){
public static void cancelNotification(Context context, int notifyId) {
getNotificationManager(context).cancel(notifyId);
}
/**
* 获取通知管理器
*
* @return
*/
public static NotificationManager getNotificationManager(Context context){
return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
public static NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
/**
* 创建一个通知渠道兼容0以上版本
*
* @param channelId
* @param channelName
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createNotificationChannel(Context context, String channelId, String channelName,boolean isVibrate,boolean isSound){
NotificationChannel channel = new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_HIGH);
public static void createNotificationChannel(Context context, String channelId, String channelName, boolean isVibrate, boolean isSound) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(isVibrate);
if(!isSound){
channel.setSound(null,null);
if (!isSound) {
channel.setSound(null, null);
}
getNotificationManager(context).createNotificationChannel(channel);
@ -201,18 +225,20 @@ public class NotificationUtils {
/**
* 构建一个通知构建器
*
* @param channelId
* @param icon
* @param title
* @param content
* @return
*/
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon,CharSequence title,CharSequence content){
return buildNotification(context,channelId,icon,title,content,Constants.NONE,Constants.NONE);
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content) {
return buildNotification(context, channelId, icon, title, content, Constants.NONE, Constants.NONE);
}
/**
* 构建一个通知构建器
*
* @param channelId
* @param icon
* @param title
@ -221,16 +247,16 @@ public class NotificationUtils {
* @param size
* @return
*/
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, int progress, int size){
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, int progress, int size) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
builder.setSmallIcon(icon);
builder.setContentTitle(title);
builder.setContentText(content);
builder.setOngoing(true);
if(progress!= Constants.NONE && size!=Constants.NONE){
builder.setProgress(size,progress,false);
if (progress != Constants.NONE && size != Constants.NONE) {
builder.setProgress(size, progress, false);
}
return builder;
@ -238,11 +264,12 @@ public class NotificationUtils {
/**
* 更新通知栏
*
* @param id
* @param notification
*/
private static void notifyNotification(Context context, int id, Notification notification){
getNotificationManager(context).notify(id,notification);
private static void notifyNotification(Context context, int id, Notification notification) {
getNotificationManager(context).notify(id, notification);
}
}

Loading…
Cancel
Save