修改所有通知栏,点击后都跳转到主窗口。

This commit is contained in:
2025-12-14 02:10:13 +08:00
parent 28d8a5679f
commit f09bb17cbc
3 changed files with 25 additions and 32 deletions

View File

@@ -262,7 +262,7 @@ public class ControlCenterService extends Service {
}
NotificationManagerUtils notificationManagerUtils = new NotificationManagerUtils(ControlCenterService.this);
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);

View File

@@ -22,7 +22,7 @@ import cc.winboll.studio.powerbell.services.ControlCenterService;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/13 20:44
* @Describe 全局通知管理工具类整合所有通知能力适配API29-30兼容Java7
* @Describe 全局通知管理工具类整合所有通知能力适配API29-30兼容Java7所有通知统一跳转MainActivity
*/
public class NotificationManagerUtils {
// ====================== 常量定义(统一管理,避免冲突,首屏可见)======================
@@ -50,7 +50,7 @@ public class NotificationManagerUtils {
? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
: PendingIntent.FLAG_UPDATE_CURRENT; // API30安全标志
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;
@@ -151,21 +151,16 @@ public class NotificationManagerUtils {
}
/**
* 构建通用跳转PendingIntent适配API29-30安全规范,支持自定义跳转目标
* @param targetIntent 自定义跳转意图传null则默认跳MainActivity
* @return 安全的PendingIntent避免跳转失败/泄露
* 构建固定跳转PendingIntent所有通知统一跳转MainActivity适配API29-30安全规范
* @return 安全的PendingIntent确保跳转稳定不泄露
*/
private PendingIntent buildPendingIntent(Intent targetIntent) {
Intent intent = targetIntent;
// 传null则默认跳MainActivity兼容旧逻辑
if (intent == null) {
intent = new Intent(mContext, MainActivity.class);
LogUtils.d(TAG, "【Intent构建】未传自定义Intent默认跳转MainActivity");
}
private PendingIntent buildFixedPendingIntent() {
// 固定跳MainActivity不支持自定义目标
Intent intent = new Intent(mContext, MainActivity.class);
// API29+ 强制要求:明确包名,避免跳转目标模糊
intent.setPackage(DEFAULT_JUMP_PACKAGE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 确保跳转生效
LogUtils.d(TAG, "【Intent构建】跳转包名:" + DEFAULT_JUMP_PACKAGE + ",目标:" + intent.getComponent().getClassName());
intent.setPackage(mContext.getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); // 跳转时清除栈顶避免重复创建Activity
LogUtils.d(TAG, "【Intent构建】所有通知统一跳转MainActivity包名" + mContext.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(
mContext,
@@ -200,8 +195,8 @@ public class NotificationManagerUtils {
return;
}
// 1. 构建跳转Intent
PendingIntent pendingIntent = buildPendingIntent(null); // 默认跳MainActivity
// 1. 构建固定跳转Intent统一跳MainActivity
PendingIntent pendingIntent = buildFixedPendingIntent();
// 2. 构建基础通知兼容API26+渠道低版本用Builder
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -282,8 +277,8 @@ public class NotificationManagerUtils {
LogUtils.e(TAG, "【电量提醒通知】初始化失败Service/NotificationMessage为空");
return;
}
// 1. 构建跳转Intent
PendingIntent pendingIntent = buildPendingIntent(null);
// 1. 构建固定跳转Intent统一跳MainActivity
PendingIntent pendingIntent = buildFixedPendingIntent();
// 2. 构建基础通知
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -337,19 +332,18 @@ public class NotificationManagerUtils {
// ====================== 场景3通用临时通知简单文本自动取消无需自定义布局======================
/**
* 显示通用临时通知(普通告警,仅震动,自动取消,支持自定义跳转目标
* @param targetIntent 自定义跳转Intent传null默认跳MainActivity
* 显示通用临时通知(普通告警,仅震动,自动取消,统一跳转MainActivity
* @param title 通知标题
* @param content 通知内容
*/
public void showTempAlertNotify(Intent targetIntent, String title, String content) {
public void showTempAlertNotify(String title, String content) {
LogUtils.d(TAG, "【通用临时通知】开始构建,标题:" + title + ",内容:" + content);
if (title == null || content == null) {
LogUtils.e(TAG, "【通用临时通知】构建失败:标题/内容为空");
return;
}
// 1. 构建跳转Intent
PendingIntent pendingIntent = buildPendingIntent(targetIntent);
// 1. 构建固定跳转Intent统一跳MainActivity
PendingIntent pendingIntent = buildFixedPendingIntent();
// 2. 用NotificationCompat.Builder兼容所有版本简化逻辑
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
.setSmallIcon(R.drawable.ic_launcher)
@@ -369,19 +363,18 @@ public class NotificationManagerUtils {
// ====================== 场景4自定义布局通知灵活扩展支持复杂样式======================
/**
* 显示自定义布局通知(支持普通布局+大布局,通用所有场景,可自定义跳转
* @param targetIntent 自定义跳转Intent
* 显示自定义布局通知(支持普通布局+大布局,通用所有场景,统一跳转MainActivity
* @param contentView 普通自定义布局(必填)
* @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));
if (contentView == null) {
LogUtils.e(TAG, "【自定义布局通知】构建失败普通布局contentView为空");
return;
}
// 1. 构建跳转Intent
PendingIntent pendingIntent = buildPendingIntent(targetIntent);
// 1. 构建固定跳转Intent统一跳MainActivity
PendingIntent pendingIntent = buildFixedPendingIntent();
// 2. 构建自定义布局通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID_TEMP_ALERT)
.setSmallIcon(R.drawable.ic_launcher) // 必传,不可省略