Android实现点击Notification通知栏,跳转指定activity页面

效果

1、点击通知栏通知,假如app正在运行,则直接跳转到指定activity显示具体内容,在指定activity中按返回键返回其上一级页面。

2、点击通知栏通知,假如app已经退出,先从SplashActivity进入,显示app启动界面,在Loginactivity判断是否登录状态完成后,进入MainActivity再跳转到指定activity显示具体内容,在指定activity中按Back键返回其上一级页面。

实现

1、定义【AndroidManifest.xml】清单

【android:parentActivityName】

通过定义上面这个属性来判断点击返回时的处理逻辑。

示例:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.lives">

    ......

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/logo"
        android:supportsRtl="true"
        android:theme="@style/Theme.Lives"
        android:usesCleartextTraffic="true"
        tools:targetApi="31">
        <!-- 启动动画页面 -->
        <activity
            android:name=".activities.SplashActivity"
            android:exported="true"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 登录页面 -->
        <activity
            android:name=".activities.LoginActivity"
            android:exported="false" />
        <!-- 主页面 -->
        <activity
            android:name=".activities.MainActivity"
            android:exported="false" />
        <!-- 一级子页面 -->
        <activity
            android:name=".activities.NoticeActivity"
            android:parentActivityName=".activities.MainActivity"
            android:exported="false" />
        <!-- 二级子页面 -->
        <activity
            android:name=".activities.NoticeItemActivity"
            android:parentActivityName=".activities.NoticeActivity"
            android:exported="false" />

        ......
    </application>

</manifest>

 注意:因为逻辑原因,我们不能在主界面点击返回时,返回到登录界面,所以LoginActivity、SplashActivity和MainActivity这3个是不能添加这个父组件参数的,我们会在后面进行处理的。

2、实现notification配置

我是在我自己的websocket服务中直接调用的,你们自己也可以封装成一个工具类,我的代码示例:

/**
     * 发送消息通知
     * @param title 通知标题
     * @param message 通知内容
     * @param type 通知类型
     * @param targetActivityClass 点击通知后要跳转的目标 Activity 类
     */
    private void sendMessageNotification(String title, String message, Class<?> targetActivityClass) {
        Intent resultIntent;
        // 判断当前app是否运行中
        if (!isAppRunning()) {
            // 如果 app 已退出,先启动 SplashActivity
            Log.d(TAG, "App 未运行,启动 SplashActivity");
            resultIntent = new Intent(this, SplashActivity.class);
            // 携带目标 Activity 信息,方便后续跳转
            resultIntent.putExtra("targetActivity", targetActivityClass.getName());
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            // 如果 app 未退出,直接使用原有目标 Activity
            Log.d(TAG, "App 运行中,直接使用目标 Activity");
            resultIntent = new Intent(this, targetActivityClass);
        }

        // 创建一个 TaskStackBuilder 实例,用于构建一个任务栈,该任务栈可以帮助管理 Activity 的启动顺序和回退行为
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // 将目标意图 resultIntent 添加到任务栈中,并根据 AndroidManifest.xml 中的父 Activity 配置构建完整的任务栈
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        // 从任务栈中获取一个 PendingIntent,该 PendingIntent 包含了任务栈中的所有意图
        // 0 表示请求码,用于唯一标识这个 PendingIntent
        // PendingIntent.FLAG_UPDATE_CURRENT 表示如果该 PendingIntent 已经存在,则更新其内部的额外数据
        PendingIntent pendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // 执行消息发送
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_launcher_background)  // 设置通知小图标
                .setContentTitle(title) // 设置通知标题
                .setContentText(message) // 设置通知内容
                .setPriority(NotificationCompat.PRIORITY_HIGH) // 设置通知优先级为高
                .setDefaults(Notification.DEFAULT_ALL) // 设置默认的声音、震动和灯光
                .setFullScreenIntent(null, true) // 对于紧急通知,可触发横幅通知
                .setContentIntent(pendingIntent) // 设置点击通知后的跳转意图
                .setAutoCancel(true); // 点击通知后自动取消通知

        // 发送通知
        notificationManager.notify((int) System.currentTimeMillis(), builder.build());
    }

    // 判断是否app当前运行中
    private boolean isAppRunning() {
        // 获取当前运行的应用程序列表
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        // 获取当前应用程序的包名
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        // 遍历应用程序列表,检查当前应用程序是否在运行中
        final String packageName = getPackageName();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND &&
                    appProcess.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
// 执行生成Notification通知
sendMessageNotification("标题", "内容", NoticeItemActivity.class);

3、在SplashActivity等未配置父组件参数的3个Activity中进行判断处理跳转

在onCreate方法中调用如下代码

// 检查是否有指定跳转的目标Activity信息(例如通知栏跳转等)
Intent intent = getIntent();
Intent loginIntent = new Intent(SplashActivity.this, LoginActivity.class);
if (intent.hasExtra("targetActivity")) {
     String targetActivityName = intent.getStringExtra("targetActivity");
     Log.d("SplashActivity", "SplashActivity检查到存在目标Activity: " + targetActivityName);
     loginIntent.putExtra("targetActivity", targetActivityName);
} else {
     Log.d("SplashActivity", "SplashActivity未检查到存在目标Activity,直接跳转登录页面");
}
startActivity(loginIntent);
finish();

其他2个Activity同理,但是MainActivity有点区别,因为他需要到指定Activity,代码如下:

// 检查是否有指定跳转的目标Activity信息(例如通知栏跳转等)
Intent intent = getIntent();
if (intent.hasExtra("targetActivity")) {
   String targetActivityName = intent.getStringExtra("targetActivity");
   Log.d(TAG, "MainActivity检查到存在目标Activity: " + targetActivityName);
   if(targetActivityName.equals("com.example.lives.activities.MainActivity")) {
        Log.d(TAG, "当前已经处于主页面,无需跳转");
   } else {
        try {
            Class<?> targetActivityClass = Class.forName(targetActivityName);
            // 使用 Handler 进行延时操作
            new android.os.Handler(android.os.Looper.getMainLooper()).postDelayed(() -> {
                 Intent targetIntent = new Intent(MainActivity.this, targetActivityClass);
                 startActivity(targetIntent);
            }, 200);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "MainActivity跳转目标Activity未实现: " + e);
        }
   }
} else {
   Log.d(TAG, "MainActivity未检查到存在目标Activity,无需操作");
}

这样就基本实现了我们想要的效果,具体一些细节体验可以继续优化,欢迎大家交流!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值