@@ -20,9 +20,9 @@ import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.models.NotificationMessage ;
/**
* 通知工具类:统一管理前台服务/电池提醒通知
* 通知工具类:统一管理前台服务/电池提醒/应用配置信息 通知
* 适配: API19-30 | Java7 | 小米手机
* 特性: 前台服务无铃声、提醒通知系统默认铃声、API分级适配、内存泄漏防护
* 特性:前台服务无铃声、提醒通知系统默认铃声、配置通知低优先级无打扰、 API分级适配、内存泄漏防护
*/
public class NotificationManagerUtils {
// ================================== 静态常量(置顶统一管理,杜绝魔法值)=================================
@@ -30,9 +30,11 @@ public class NotificationManagerUtils {
// 通知渠道ID( API26+ 必需,区分通知类型)
public static final String CHANNEL_ID_FOREGROUND = " cc.winboll.studio.powerbell.channel.foreground " ;
public static final String CHANNEL_ID_REMIND = " cc.winboll.studio.powerbell.channel.remind " ;
public static final String CHANNEL_ID_CONFIG = " cc.winboll.studio.powerbell.channel.config " ; // 新增:应用配置信息渠道
// 通知ID( 唯一标识, 避免重复)
public static final int NOTIFY_ID_FOREGROUND_SERVICE = 1001 ;
public static final int NOTIFY_ID_REMIND = 1002 ;
public static final int NOTIFY_ID_CONFIG = 1003 ; // 新增: 应用配置信息通知ID
// 低版本兼容: 默认通知图标( API<21 避免显示异常)
private static final int NOTIFICATION_DEFAULT_ICON = R . drawable . ic_launcher ;
// 通知内容兜底常量
@@ -40,9 +42,12 @@ public class NotificationManagerUtils {
private static final String FOREGROUND_NOTIFY_CONTENT_DEFAULT = " 后台监测电池状态 " ;
private static final String REMIND_NOTIFY_TITLE_DEFAULT = " 电池状态提醒 " ;
private static final String REMIND_NOTIFY_CONTENT_DEFAULT = " 电池状态异常,请及时处理 " ;
private static final String CONFIG_NOTIFY_TITLE_DEFAULT = " 应用配置更新 " ; // 新增:配置通知默认标题
private static final String CONFIG_NOTIFY_CONTENT_DEFAULT = " 配置信息已更新,生效中 " ; // 新增:配置通知默认内容
// PendingIntent请求码
private static final int PENDING_INTENT_REQUEST_CODE_FOREGROUND = 0 ;
private static final int PENDING_INTENT_REQUEST_CODE_REMIND = 1 ;
private static final int PENDING_INTENT_REQUEST_CODE_CONFIG = 2 ; // 新增:配置通知请求码
// ================================== 成员变量(私有封装,按依赖优先级排序)=================================
// 核心上下文(应用级,避免内存泄漏)
@@ -54,33 +59,35 @@ public class NotificationManagerUtils {
// ================================== 构造方法(初始化核心资源,前置校验)=================================
public NotificationManagerUtils ( Context context ) {
LogUtils . d ( TAG , " 构造方法执行 " ) ;
LogUtils . d ( TAG , " NotificationManagerUtils: 构造方法执行 | context= " + context ) ;
// 前置校验: Context非空
if ( context = = null ) {
LogUtils . e ( TAG , " 构造失败: context is null " ) ;
LogUtils . e ( TAG , " NotificationManagerUtils: 构造失败: context is null" ) ;
return ;
}
// 初始化核心资源
this . mContext = context . getApplicationContext ( ) ;
this . mNotificationManager = ( NotificationManager ) mContext . getSystemService ( Context . NOTIFICATION_SERVICE ) ;
LogUtils . d ( TAG , " NotificationManagerUtils: 核心资源初始化完成 | mContext= " + mContext + " | mNotificationManager= " + mNotificationManager ) ;
// 初始化通知渠道( API26+ 必需)
initNotificationChannels ( ) ;
LogUtils . d ( TAG , " 构造完成:核心资源初始化成功 " ) ;
LogUtils . d ( TAG , " NotificationManagerUtils: 构造完成" ) ;
}
// ================================== 核心初始化方法( 通知渠道, API分级适配) =================================
/**
* 初始化通知渠道:前台服务渠道(无铃声+无振动)、提醒渠道(系统默认铃声+无振动)
* 初始化通知渠道:前台服务渠道(无铃声+无振动)、提醒渠道(系统默认铃声+无振动)、配置信息渠道(低优先级无打扰)
*/
private void initNotificationChannels ( ) {
LogUtils . d ( TAG , " initNotificationChannels: 执行通知渠道初始化 " ) ;
// API<26 无渠道机制,直接返回
if ( Build . VERSION . SDK_INT < Build . VERSION_CODES . O ) {
LogUtils . d ( TAG , " initNotificationChannels: API<26, 无需创建渠道 " ) ;
LogUtils . d ( TAG , " initNotificationChannels: API<26, 无需创建渠道 " ) ;
return ;
}
// 通知服务为空,避免空指针
if ( mNotificationManager = = null ) {
LogUtils . e ( TAG , " initNotificationChannels失败: NotificationManager is null " ) ;
LogUtils . e ( TAG , " initNotificationChannels: 失败: NotificationManager is null " ) ;
return ;
}
@@ -96,6 +103,7 @@ public class NotificationManagerUtils {
foregroundChannel . setSound ( null , null ) ; // 强制无铃声
foregroundChannel . setShowBadge ( false ) ;
foregroundChannel . setLockscreenVisibility ( Notification . VISIBILITY_SECRET ) ;
LogUtils . d ( TAG , " initNotificationChannels: 前台服务渠道配置完成 " ) ;
// 2. 电池提醒渠道(中优先级,系统默认铃声,无振动)
NotificationChannel remindChannel = new NotificationChannel (
@@ -109,11 +117,27 @@ public class NotificationManagerUtils {
remindChannel . setSound ( RingtoneManager . getDefaultUri ( RingtoneManager . TYPE_NOTIFICATION ) , Notification . AUDIO_ATTRIBUTES_DEFAULT ) ;
remindChannel . setShowBadge ( false ) ;
remindChannel . setLockscreenVisibility ( Notification . VISIBILITY_PUBLIC ) ;
LogUtils . d ( TAG , " initNotificationChannels: 电池提醒渠道配置完成 " ) ;
// 3. 应用配置信息渠道(新增:最低优先级,无铃声无振动,仅提示不打扰)
NotificationChannel configChannel = new NotificationChannel (
CHANNEL_ID_CONFIG ,
" 应用配置信息 " ,
NotificationManager . IMPORTANCE_MIN
) ;
configChannel . setDescription ( " 应用配置更新、参数变更等提示,无声音、无振动 " ) ;
configChannel . enableLights ( false ) ;
configChannel . enableVibration ( false ) ;
configChannel . setSound ( null , null ) ;
configChannel . setShowBadge ( false ) ;
configChannel . setLockscreenVisibility ( Notification . VISIBILITY_PRIVATE ) ;
LogUtils . d ( TAG , " initNotificationChannels: 应用配置信息渠道配置完成 " ) ;
// 注册渠道到系统
mNotificationManager . createNotificationChannel ( foregroundChannel ) ;
mNotificationManager . createNotificationChannel ( remindChannel ) ;
LogUtils . d ( TAG , " initNotificationChannels成功: 创建前台服务+电池提醒渠道 " ) ;
mNotificationManager . createNotificationChannel ( configChannel ) ; // 注册新增渠道
LogUtils . d ( TAG , " initNotificationChannels: 成功:创建前台服务+电池提醒+应用配置信息渠道 " ) ;
}
// ================================== 对外核心方法(前台服务通知:启动/更新/取消)=================================
@@ -121,26 +145,26 @@ public class NotificationManagerUtils {
* 启动前台服务通知( API30适配, 无铃声)
*/
public void startForegroundServiceNotify ( Service service , NotificationMessage message ) {
LogUtils . d ( TAG , " startForegroundServiceNotify执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE ) ;
LogUtils . d ( TAG , " startForegroundServiceNotify: 执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE + " | service= " + service + " | message= " + message ) ;
// 前置校验:参数非空
if ( service = = null | | message = = null | | mNotificationManager = = null ) {
LogUtils . e ( TAG , " startForegroundServiceNotify失败: param is null | service= " + service + " | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
LogUtils . e ( TAG , " startForegroundServiceNotify: 失败: param is null | service= " + service + " | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
return ;
}
// 构建前台通知
mForegroundServiceNotify = buildForegroundNotification ( message ) ;
if ( mForegroundServiceNotify = = null ) {
LogUtils . e ( TAG , " startForegroundServiceNotify失败: 构建通知为空 " ) ;
LogUtils . e ( TAG , " startForegroundServiceNotify: 失败:构建通知为空 " ) ;
return ;
}
// 启动前台服务( API30无FOREGROUND_SERVICE_TYPE限制, 全版本通用)
try {
service . startForeground ( NOTIFY_ID_FOREGROUND_SERVICE , mForegroundServiceNotify ) ;
LogUtils . d ( TAG , " startForegroundServiceNotify成功 " ) ;
LogUtils . d ( TAG , " startForegroundServiceNotify: 成功 " ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " startForegroundServiceNotify异常 " , e ) ;
LogUtils . e ( TAG , " startForegroundServiceNotify: 异常 " , e ) ;
}
}
@@ -148,23 +172,23 @@ public class NotificationManagerUtils {
* 更新前台服务通知内容( 复用通知ID, 保持无铃声)
*/
public void updateForegroundServiceNotify ( NotificationMessage message ) {
LogUtils . d ( TAG , " updateForegroundServiceNotify执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE ) ;
LogUtils . d ( TAG , " updateForegroundServiceNotify: 执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE + " | message= " + message ) ;
if ( message = = null | | mNotificationManager = = null ) {
LogUtils . e ( TAG , " updateForegroundServiceNotify失败: param is null | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
LogUtils . e ( TAG , " updateForegroundServiceNotify: 失败: param is null | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
return ;
}
mForegroundServiceNotify = buildForegroundNotification ( message ) ;
if ( mForegroundServiceNotify = = null ) {
LogUtils . e ( TAG , " updateForegroundServiceNotify失败: 构建通知为空 " ) ;
LogUtils . e ( TAG , " updateForegroundServiceNotify: 失败:构建通知为空 " ) ;
return ;
}
try {
mNotificationManager . notify ( NOTIFY_ID_FOREGROUND_SERVICE , mForegroundServiceNotify ) ;
LogUtils . d ( TAG , " updateForegroundServiceNotify成功 " ) ;
LogUtils . d ( TAG , " updateForegroundServiceNotify: 成功 " ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " updateForegroundServiceNotify异常 " , e ) ;
LogUtils . e ( TAG , " updateForegroundServiceNotify: 异常 " , e ) ;
}
}
@@ -172,10 +196,10 @@ public class NotificationManagerUtils {
* 取消前台服务通知( Service销毁时调用)
*/
public void cancelForegroundServiceNotify ( ) {
LogUtils . d ( TAG , " cancelForegroundServiceNotify执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE ) ;
LogUtils . d ( TAG , " cancelForegroundServiceNotify: 执行 | notifyId= " + NOTIFY_ID_FOREGROUND_SERVICE ) ;
cancelNotification ( NOTIFY_ID_FOREGROUND_SERVICE ) ;
mForegroundServiceNotify = null ; // 置空释放
LogUtils . d ( TAG , " cancelForegroundServiceNotify成功 " ) ;
LogUtils . d ( TAG , " cancelForegroundServiceNotify: 成功 " ) ;
}
// ================================== 对外核心方法(电池提醒通知:发送)=================================
@@ -183,23 +207,48 @@ public class NotificationManagerUtils {
* 发送电池提醒通知(系统默认铃声,无振动)
*/
public void showRemindNotification ( Context context , NotificationMessage message ) {
LogUtils . d ( TAG , " showRemindNotification执行 | notifyId= " + NOTIFY_ID_REMIND ) ;
LogUtils . d ( TAG , " showRemindNotification: 执行 | notifyId= " + NOTIFY_ID_REMIND + " | context= " + context + " | message= " + message ) ;
if ( context = = null | | message = = null | | mNotificationManager = = null ) {
LogUtils . e ( TAG , " showRemindNotification失败: param is null | context= " + context + " | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
LogUtils . e ( TAG , " showRemindNotification: 失败: param is null | context= " + context + " | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
return ;
}
Notification remindNotify = buildRemindNotification ( context , message ) ;
if ( remindNotify = = null ) {
LogUtils . e ( TAG , " showRemindNotification失败: 构建通知为空 " ) ;
LogUtils . e ( TAG , " showRemindNotification: 失败:构建通知为空 " ) ;
return ;
}
try {
mNotificationManager . notify ( NOTIFY_ID_REMIND , remindNotify ) ;
LogUtils . d ( TAG , " showRemindNotification成功 " ) ;
LogUtils . d ( TAG , " showRemindNotification: 成功 " ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " showRemindNotification异常 " , e ) ;
LogUtils . e ( TAG , " showRemindNotification: 异常 " , e ) ;
}
}
// ================================== 对外核心方法(应用配置信息通知:发送)=================================
/**
* 发送应用配置信息通知(新增:低优先级无铃声,仅提示不打扰)
*/
public void showConfigNotification ( Context context , NotificationMessage message ) {
LogUtils . d ( TAG , " showConfigNotification: 执行 | notifyId= " + NOTIFY_ID_CONFIG + " | context= " + context + " | message= " + message ) ;
if ( context = = null | | message = = null | | mNotificationManager = = null ) {
LogUtils . e ( TAG , " showConfigNotification: 失败: param is null | context= " + context + " | message= " + message + " | mNotificationManager= " + mNotificationManager ) ;
return ;
}
Notification configNotify = buildConfigNotification ( context , message ) ;
if ( configNotify = = null ) {
LogUtils . e ( TAG , " showConfigNotification: 失败:构建通知为空 " ) ;
return ;
}
try {
mNotificationManager . notify ( NOTIFY_ID_CONFIG , configNotify ) ;
LogUtils . d ( TAG , " showConfigNotification: 成功 " ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " showConfigNotification: 异常 " , e ) ;
}
}
@@ -208,16 +257,16 @@ public class NotificationManagerUtils {
* 取消指定ID的通知
*/
public void cancelNotification ( int notifyId ) {
LogUtils . d ( TAG , " cancelNotification执行 | notifyId= " + notifyId ) ;
LogUtils . d ( TAG , " cancelNotification: 执行 | notifyId= " + notifyId ) ;
if ( mNotificationManager = = null ) {
LogUtils . e ( TAG , " cancelNotification失败: NotificationManager is null " ) ;
LogUtils . e ( TAG , " cancelNotification: 失败: NotificationManager is null " ) ;
return ;
}
try {
mNotificationManager . cancel ( notifyId ) ;
LogUtils . d ( TAG , " cancelNotification成功 | notifyId= " + notifyId ) ;
LogUtils . d ( TAG , " cancelNotification: 成功 | notifyId= " + notifyId ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " cancelNotification异常 | notifyId= " + notifyId , e ) ;
LogUtils . e ( TAG , " cancelNotification: 异常 | notifyId= " + notifyId , e ) ;
}
}
@@ -225,16 +274,16 @@ public class NotificationManagerUtils {
* 取消所有通知(兜底场景使用)
*/
public void cancelAllNotifications ( ) {
LogUtils . d ( TAG , " cancelAllNotifications执行 " ) ;
LogUtils . d ( TAG , " cancelAllNotifications: 执行 " ) ;
if ( mNotificationManager = = null ) {
LogUtils . e ( TAG , " cancelAllNotifications失败: NotificationManager is null " ) ;
LogUtils . e ( TAG , " cancelAllNotifications: 失败: NotificationManager is null " ) ;
return ;
}
try {
mNotificationManager . cancelAll ( ) ;
LogUtils . d ( TAG , " cancelAllNotifications成功 " ) ;
LogUtils . d ( TAG , " cancelAllNotifications: 成功 " ) ;
} catch ( Exception e ) {
LogUtils . e ( TAG , " cancelAllNotifications异常 " , e ) ;
LogUtils . e ( TAG , " cancelAllNotifications: 异常 " , e ) ;
}
}
@@ -243,28 +292,30 @@ public class NotificationManagerUtils {
* 构建前台服务通知(全版本无铃声+无振动)
*/
private Notification buildForegroundNotification ( NotificationMessage message ) {
LogUtils . d ( TAG , " buildForegroundNotification执行 " ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 执行 | message= " + message ) ;
if ( message = = null | | mContext = = null ) {
LogUtils . e ( TAG , " buildForegroundNotification失败: param is null | message= " + message + " | mContext= " + mContext ) ;
LogUtils . e ( TAG , " buildForegroundNotification: 失败: param is null | message= " + message + " | mContext= " + mContext ) ;
return null ;
}
// 内容兜底
String title = message . getTitle ( ) ! = null & & ! message . getTitle ( ) . isEmpty ( ) ? message . getTitle ( ) : FOREGROUND_NOTIFY_TITLE_DEFAULT ;
String content = message . getContent ( ) ! = null & & ! message . getContent ( ) . isEmpty ( ) ? message . getContent ( ) : FOREGROUND_NOTIFY_CONTENT_DEFAULT ;
LogUtils . d ( TAG , " buildForegroundNotification: title= " + title + " | content= " + content ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 内容兜底完成 | title= " + title + " | content= " + content ) ;
Notification . Builder builder ;
// API分级构建
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
// API26+:绑定前台渠道(渠道已配置无铃声)
builder = new Notification . Builder ( mContext , CHANNEL_ID_FOREGROUND ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 使用API26+渠道构建 " ) ;
} else {
// API<26: 直接构建, 手动禁用铃声振动
builder = new Notification . Builder ( mContext ) ;
builder . setSound ( null ) ;
builder . setVibrate ( new long [ ] { 0 } ) ;
builder . setDefaults ( 0 ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 使用API<26手动配置 " ) ;
}
// 通用配置
@@ -281,9 +332,12 @@ public class NotificationManagerUtils {
builder . setLargeIcon ( getAppIcon ( mContext ) )
. setColor ( mContext . getResources ( ) . getColor ( R . color . colorPrimary ) )
. setPriority ( Notification . PRIORITY_LOW ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 补充API21+配置 " ) ;
}
return builder . build ( ) ;
Notification notification = builder . build ( ) ;
LogUtils . d ( TAG , " buildForegroundNotification: 成功构建前台通知 " ) ;
return notification ;
}
// ================================== 内部辅助方法(通知构建:电池提醒通知)=================================
@@ -291,28 +345,30 @@ public class NotificationManagerUtils {
* 构建电池提醒通知(全版本系统默认铃声+无振动)
*/
private Notification buildRemindNotification ( Context context , NotificationMessage message ) {
LogUtils . d ( TAG , " buildRemindNotification执行 " ) ;
LogUtils . d ( TAG , " buildRemindNotification: 执行 | context= " + context + " | message= " + message ) ;
if ( context = = null | | message = = null ) {
LogUtils . e ( TAG , " buildRemindNotification失败: param is null | context= " + context + " | message= " + message ) ;
LogUtils . e ( TAG , " buildRemindNotification: 失败: param is null | context= " + context + " | message= " + message ) ;
return null ;
}
// 内容兜底
String title = message . getTitle ( ) ! = null & & ! message . getTitle ( ) . isEmpty ( ) ? message . getTitle ( ) : REMIND_NOTIFY_TITLE_DEFAULT ;
String content = message . getContent ( ) ! = null & & ! message . getContent ( ) . isEmpty ( ) ? message . getContent ( ) : REMIND_NOTIFY_CONTENT_DEFAULT ;
LogUtils . d ( TAG , " buildRemindNotification: title= " + title + " | content= " + content ) ;
LogUtils . d ( TAG , " buildRemindNotification: 内容兜底完成 | title= " + title + " | content= " + content ) ;
Notification . Builder builder ;
// API分级构建
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
// API26+:绑定提醒渠道(渠道已配置默认铃声)
builder = new Notification . Builder ( context , CHANNEL_ID_REMIND ) ;
LogUtils . d ( TAG , " buildRemindNotification: 使用API26+渠道构建 " ) ;
} else {
// API<26: 手动配置默认铃声, 关闭振动
builder = new Notification . Builder ( context ) ;
builder . setSound ( Settings . System . DEFAULT_NOTIFICATION_URI ) // 显式默认铃声
. setVibrate ( new long [ ] { 0 } )
. setDefaults ( Notification . DEFAULT_LIGHTS | Notification . DEFAULT_SOUND ) ;
LogUtils . d ( TAG , " buildRemindNotification: 使用API<26手动配置 " ) ;
}
// 通用配置
@@ -330,9 +386,65 @@ public class NotificationManagerUtils {
builder . setLargeIcon ( getAppIcon ( context ) )
. setColor ( context . getResources ( ) . getColor ( R . color . colorPrimary ) )
. setPriority ( Notification . PRIORITY_DEFAULT ) ;
LogUtils . d ( TAG , " buildRemindNotification: 补充API21+配置 " ) ;
}
return builder . build ( ) ;
Notification notification = builder . build ( ) ;
LogUtils . d ( TAG , " buildRemindNotification: 成功构建提醒通知 " ) ;
return notification ;
}
// ================================== 内部辅助方法(通知构建:应用配置信息通知)=================================
/**
* 构建应用配置信息通知(新增:全版本无铃声+无振动,低优先级)
*/
private Notification buildConfigNotification ( Context context , NotificationMessage message ) {
LogUtils . d ( TAG , " buildConfigNotification: 执行 | context= " + context + " | message= " + message ) ;
if ( context = = null | | message = = null ) {
LogUtils . e ( TAG , " buildConfigNotification: 失败: param is null | context= " + context + " | message= " + message ) ;
return null ;
}
// 内容兜底
String title = message . getTitle ( ) ! = null & & ! message . getTitle ( ) . isEmpty ( ) ? message . getTitle ( ) : CONFIG_NOTIFY_TITLE_DEFAULT ;
String content = message . getContent ( ) ! = null & & ! message . getContent ( ) . isEmpty ( ) ? message . getContent ( ) : CONFIG_NOTIFY_CONTENT_DEFAULT ;
LogUtils . d ( TAG , " buildConfigNotification: 内容兜底完成 | title= " + title + " | content= " + content ) ;
Notification . Builder builder ;
// API分级构建
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . O ) {
// API26+:绑定配置渠道(渠道已配置无铃声)
builder = new Notification . Builder ( context , CHANNEL_ID_CONFIG ) ;
LogUtils . d ( TAG , " buildConfigNotification: 使用API26+渠道构建 " ) ;
} else {
// API<26: 直接构建, 手动禁用铃声振动
builder = new Notification . Builder ( context ) ;
builder . setSound ( null ) ;
builder . setVibrate ( new long [ ] { 0 } ) ;
builder . setDefaults ( 0 ) ;
LogUtils . d ( TAG , " buildConfigNotification: 使用API<26手动配置 " ) ;
}
// 通用配置
builder . setSmallIcon ( NOTIFICATION_DEFAULT_ICON )
. setContentTitle ( title )
. setContentText ( content )
. setAutoCancel ( true ) // 点击关闭
. setOngoing ( false )
. setWhen ( System . currentTimeMillis ( ) )
. setContentIntent ( createJumpPendingIntent ( context , PENDING_INTENT_REQUEST_CODE_CONFIG ) ) ;
// API21+ 新增大图标+主题色
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . LOLLIPOP ) {
builder . setLargeIcon ( getAppIcon ( context ) )
. setColor ( context . getResources ( ) . getColor ( R . color . colorPrimary ) )
. setPriority ( Notification . PRIORITY_MIN ) ; // 最低优先级
LogUtils . d ( TAG , " buildConfigNotification: 补充API21+配置 " ) ;
}
Notification notification = builder . build ( ) ;
LogUtils . d ( TAG , " buildConfigNotification: 成功构建配置信息通知 " ) ;
return notification ;
}
// ================================== 内部辅助方法( 创建跳转PendingIntent, API30安全适配) =================================
@@ -340,19 +452,20 @@ public class NotificationManagerUtils {
* 创建跳转MainActivity的PendingIntent, API23+ 添加IMMUTABLE标记( 避免安全异常)
*/
private PendingIntent createJumpPendingIntent ( Context context , int requestCode ) {
LogUtils . d ( TAG , " createJumpPendingIntent执行 | requestCode= " + requestCode ) ;
LogUtils . d ( TAG , " createJumpPendingIntent: 执行 | requestCode= " + requestCode + " | context= " + context ) ;
Intent intent = new Intent ( context , MainActivity . class ) ;
intent . addFlags ( Intent . FLAG_ACTIVITY_NEW_TASK | Intent . FLAG_ACTIVITY_CLEAR_TOP ) ;
LogUtils . d ( TAG , " createJumpPendingIntent: 跳转Intent配置完成 " ) ;
// API23+ 必需添加IMMUTABLE, 适配API30安全规范
int flags = PendingIntent . FLAG_UPDATE_CURRENT ;
if ( Build . VERSION . SDK_INT > = Build . VERSION_CODES . M ) {
flags | = PendingIntent . FLAG_IMMUTABLE ;
LogUtils . d ( TAG , " createJumpPendingIntent: 添加FLAG_IMMUTABLE标记 " ) ;
LogUtils . d ( TAG , " createJumpPendingIntent: 添加FLAG_IMMUTABLE标记( API23+) " ) ;
}
PendingIntent pendingIntent = PendingIntent . getActivity ( context , requestCode , intent , flags ) ;
LogUtils . d ( TAG , " createJumpPendingIntent成功 | requestCode= " + requestCode ) ;
LogUtils . d ( TAG , " createJumpPendingIntent: 成功 | requestCode= " + requestCode ) ;
return pendingIntent ;
}
@@ -361,33 +474,33 @@ public class NotificationManagerUtils {
* 获取APP图标, 失败返回默认图标
*/
private Bitmap getAppIcon ( Context context ) {
LogUtils . d ( TAG , " getAppIcon执行 " ) ;
LogUtils . d ( TAG , " getAppIcon: 执行 | context= " + context ) ;
try {
PackageInfo pkgInfo = context . getPackageManager ( ) . getPackageInfo ( context . getPackageName ( ) , 0 ) ;
Bitmap appIcon = BitmapFactory . decodeResource ( context . getResources ( ) , pkgInfo . applicationInfo . icon ) ;
LogUtils . d ( TAG , " getAppIcon成功: 获取应用图标 " ) ;
LogUtils . d ( TAG , " getAppIcon: 成功:获取应用图标 " ) ;
return appIcon ;
} catch ( PackageManager . NameNotFoundException e ) {
LogUtils . e ( TAG , " getAppIcon异常: 获取应用图标失败, 使用默认图标 " , e ) ;
LogUtils . e ( TAG , " getAppIcon: 异常:获取应用图标失败,使用默认图标 " , e ) ;
return BitmapFactory . decodeResource ( context . getResources ( ) , NOTIFICATION_DEFAULT_ICON ) ;
}
}
// ================================== 对外 getter 方法(仅前台通知实例,只读)=================================
public Notification getForegroundServiceNotify ( ) {
return mForegroundServiceNotify ;
}
// ================================== 资源释放方法(避免内存泄漏)=================================
/**
* 释放资源,销毁时调用
*/
public void release ( ) {
LogUtils . d ( TAG , " release执行 " ) ;
LogUtils . d ( TAG , " release: 执行资源释放 " ) ;
cancelForegroundServiceNotify ( ) ;
mNotificationManager = null ;
mContext = null ;
LogUtils . d ( TAG , " release成功: 所有资源已释放 " ) ;
LogUtils . d ( TAG , " release: 成功:所有资源已释放 " ) ;
}
// ================================== 对外 getter 方法(仅前台通知实例,只读)=================================
public Notification getForegroundServiceNotify ( ) {
return mForegroundServiceNotify ;
}
}