修改所有通知栏,点击后都跳转到主窗口。
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
#Created by .winboll/winboll_app_build.gradle
|
#Created by .winboll/winboll_app_build.gradle
|
||||||
#Sat Dec 13 21:16:25 HKT 2025
|
#Sat Dec 13 18:07:45 GMT 2025
|
||||||
stageCount=2
|
stageCount=2
|
||||||
libraryProject=
|
libraryProject=
|
||||||
baseVersion=15.14
|
baseVersion=15.14
|
||||||
publishVersion=15.14.1
|
publishVersion=15.14.1
|
||||||
buildCount=0
|
buildCount=3
|
||||||
baseBetaVersion=15.14.2
|
baseBetaVersion=15.14.2
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ public class ControlCenterService extends Service {
|
|||||||
}
|
}
|
||||||
NotificationManagerUtils notificationManagerUtils = new NotificationManagerUtils(ControlCenterService.this);
|
NotificationManagerUtils notificationManagerUtils = new NotificationManagerUtils(ControlCenterService.this);
|
||||||
Intent intent = new Intent(ControlCenterService.this, MainActivity.class);
|
Intent intent = new Intent(ControlCenterService.this, MainActivity.class);
|
||||||
notificationManagerUtils.showTempAlertNotify(intent, getString(R.string.app_name), msg);
|
notificationManagerUtils.showTempAlertNotify(getString(R.string.app_name), msg);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import cc.winboll.studio.powerbell.services.ControlCenterService;
|
|||||||
/**
|
/**
|
||||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||||
* @Date 2025/12/13 20:44
|
* @Date 2025/12/13 20:44
|
||||||
* @Describe 全局通知管理工具类(整合所有通知能力,适配API29-30,兼容Java7)
|
* @Describe 全局通知管理工具类(整合所有通知能力,适配API29-30,兼容Java7,所有通知统一跳转MainActivity)
|
||||||
*/
|
*/
|
||||||
public class NotificationManagerUtils {
|
public class NotificationManagerUtils {
|
||||||
// ====================== 常量定义(统一管理,避免冲突,首屏可见)======================
|
// ====================== 常量定义(统一管理,避免冲突,首屏可见)======================
|
||||||
@@ -50,7 +50,7 @@ public class NotificationManagerUtils {
|
|||||||
? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
||||||
: PendingIntent.FLAG_UPDATE_CURRENT; // API30安全标志
|
: PendingIntent.FLAG_UPDATE_CURRENT; // API30安全标志
|
||||||
private static final long[] VIBRATE_PATTERN = new long[]{100, 200, 300, 400}; // 标准震动节奏
|
private static final long[] VIBRATE_PATTERN = new long[]{100, 200, 300, 400}; // 标准震动节奏
|
||||||
private static final String DEFAULT_JUMP_PACKAGE = "cc.winboll.studio.powerbell"; // 默认跳转包名(API29+必填)
|
//private static final String DEFAULT_JUMP_PACKAGE = "cc.winboll.studio.powerbell"; // 默认跳转包名(API29+必填)
|
||||||
|
|
||||||
// ====================== 成员变量(按场景分组,私有封装,避免外部篡改)======================
|
// ====================== 成员变量(按场景分组,私有封装,避免外部篡改)======================
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
@@ -151,21 +151,16 @@ public class NotificationManagerUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构建通用跳转PendingIntent(适配API29-30安全规范,支持自定义跳转目标)
|
* 构建固定跳转PendingIntent(所有通知统一跳转MainActivity,适配API29-30安全规范)
|
||||||
* @param targetIntent 自定义跳转意图(传null则默认跳MainActivity)
|
* @return 安全的PendingIntent,确保跳转稳定不泄露
|
||||||
* @return 安全的PendingIntent,避免跳转失败/泄露
|
|
||||||
*/
|
*/
|
||||||
private PendingIntent buildPendingIntent(Intent targetIntent) {
|
private PendingIntent buildFixedPendingIntent() {
|
||||||
Intent intent = targetIntent;
|
// 固定跳MainActivity,不支持自定义目标
|
||||||
// 传null则默认跳MainActivity(兼容旧逻辑)
|
Intent intent = new Intent(mContext, MainActivity.class);
|
||||||
if (intent == null) {
|
|
||||||
intent = new Intent(mContext, MainActivity.class);
|
|
||||||
LogUtils.d(TAG, "【Intent构建】未传自定义Intent,默认跳转:MainActivity");
|
|
||||||
}
|
|
||||||
// API29+ 强制要求:明确包名,避免跳转目标模糊
|
// API29+ 强制要求:明确包名,避免跳转目标模糊
|
||||||
intent.setPackage(DEFAULT_JUMP_PACKAGE);
|
intent.setPackage(mContext.getPackageName());
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 确保跳转生效
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); // 跳转时清除栈顶,避免重复创建Activity
|
||||||
LogUtils.d(TAG, "【Intent构建】跳转包名:" + DEFAULT_JUMP_PACKAGE + ",目标:" + intent.getComponent().getClassName());
|
LogUtils.d(TAG, "【Intent构建】所有通知统一跳转:MainActivity,包名:" + mContext.getPackageName());
|
||||||
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(
|
PendingIntent pendingIntent = PendingIntent.getActivity(
|
||||||
mContext,
|
mContext,
|
||||||
@@ -200,8 +195,8 @@ public class NotificationManagerUtils {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. 构建跳转Intent
|
// 1. 构建固定跳转Intent(统一跳MainActivity)
|
||||||
PendingIntent pendingIntent = buildPendingIntent(null); // 默认跳MainActivity
|
PendingIntent pendingIntent = buildFixedPendingIntent();
|
||||||
// 2. 构建基础通知(兼容API26+渠道,低版本用Builder)
|
// 2. 构建基础通知(兼容API26+渠道,低版本用Builder)
|
||||||
Notification.Builder builder;
|
Notification.Builder builder;
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@@ -282,8 +277,8 @@ public class NotificationManagerUtils {
|
|||||||
LogUtils.e(TAG, "【电量提醒通知】初始化失败:Service/NotificationMessage为空");
|
LogUtils.e(TAG, "【电量提醒通知】初始化失败:Service/NotificationMessage为空");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 1. 构建跳转Intent
|
// 1. 构建固定跳转Intent(统一跳MainActivity)
|
||||||
PendingIntent pendingIntent = buildPendingIntent(null);
|
PendingIntent pendingIntent = buildFixedPendingIntent();
|
||||||
// 2. 构建基础通知
|
// 2. 构建基础通知
|
||||||
Notification.Builder builder;
|
Notification.Builder builder;
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
@@ -337,19 +332,18 @@ public class NotificationManagerUtils {
|
|||||||
|
|
||||||
// ====================== 场景3:通用临时通知(简单文本,自动取消,无需自定义布局)======================
|
// ====================== 场景3:通用临时通知(简单文本,自动取消,无需自定义布局)======================
|
||||||
/**
|
/**
|
||||||
* 显示通用临时通知(普通告警,仅震动,自动取消,支持自定义跳转目标)
|
* 显示通用临时通知(普通告警,仅震动,自动取消,统一跳转MainActivity)
|
||||||
* @param targetIntent 自定义跳转Intent(传null默认跳MainActivity)
|
|
||||||
* @param title 通知标题
|
* @param title 通知标题
|
||||||
* @param content 通知内容
|
* @param content 通知内容
|
||||||
*/
|
*/
|
||||||
public void showTempAlertNotify(Intent targetIntent, String title, String content) {
|
public void showTempAlertNotify(String title, String content) {
|
||||||
LogUtils.d(TAG, "【通用临时通知】开始构建,标题:" + title + ",内容:" + content);
|
LogUtils.d(TAG, "【通用临时通知】开始构建,标题:" + title + ",内容:" + content);
|
||||||
if (title == null || content == null) {
|
if (title == null || content == null) {
|
||||||
LogUtils.e(TAG, "【通用临时通知】构建失败:标题/内容为空");
|
LogUtils.e(TAG, "【通用临时通知】构建失败:标题/内容为空");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 1. 构建跳转Intent
|
// 1. 构建固定跳转Intent(统一跳MainActivity)
|
||||||
PendingIntent pendingIntent = buildPendingIntent(targetIntent);
|
PendingIntent pendingIntent = buildFixedPendingIntent();
|
||||||
// 2. 用NotificationCompat.Builder(兼容所有版本,简化逻辑)
|
// 2. 用NotificationCompat.Builder(兼容所有版本,简化逻辑)
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_launcher)
|
||||||
@@ -369,19 +363,18 @@ public class NotificationManagerUtils {
|
|||||||
|
|
||||||
// ====================== 场景4:自定义布局通知(灵活扩展,支持复杂样式)======================
|
// ====================== 场景4:自定义布局通知(灵活扩展,支持复杂样式)======================
|
||||||
/**
|
/**
|
||||||
* 显示自定义布局通知(支持普通布局+大布局,通用所有场景,可自定义跳转)
|
* 显示自定义布局通知(支持普通布局+大布局,通用所有场景,统一跳转MainActivity)
|
||||||
* @param targetIntent 自定义跳转Intent
|
|
||||||
* @param contentView 普通自定义布局(必填)
|
* @param contentView 普通自定义布局(必填)
|
||||||
* @param bigContentView 下拉大布局(可选)
|
* @param bigContentView 下拉大布局(可选)
|
||||||
*/
|
*/
|
||||||
public void showCustomLayoutNotify(Intent targetIntent, RemoteViews contentView, RemoteViews bigContentView) {
|
public void showCustomLayoutNotify(RemoteViews contentView, RemoteViews bigContentView) {
|
||||||
LogUtils.d(TAG, "【自定义布局通知】开始构建,布局ID:" + (contentView != null ? contentView.getLayoutId() : null));
|
LogUtils.d(TAG, "【自定义布局通知】开始构建,布局ID:" + (contentView != null ? contentView.getLayoutId() : null));
|
||||||
if (contentView == null) {
|
if (contentView == null) {
|
||||||
LogUtils.e(TAG, "【自定义布局通知】构建失败:普通布局contentView为空");
|
LogUtils.e(TAG, "【自定义布局通知】构建失败:普通布局contentView为空");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 1. 构建跳转Intent
|
// 1. 构建固定跳转Intent(统一跳MainActivity)
|
||||||
PendingIntent pendingIntent = buildPendingIntent(targetIntent);
|
PendingIntent pendingIntent = buildFixedPendingIntent();
|
||||||
// 2. 构建自定义布局通知
|
// 2. 构建自定义布局通知
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
|
||||||
.setSmallIcon(R.drawable.ic_launcher) // 必传,不可省略
|
.setSmallIcon(R.drawable.ic_launcher) // 必传,不可省略
|
||||||
|
|||||||
Reference in New Issue
Block a user