You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android全类型通知点击事件追踪方案咨询

多类型通知点击行为追踪解决方案

针对你的遗留App通知追踪需求,以下是几种低侵入、可区分来源的可行方案:

方案1:中间层BroadcastReceiver(优先推荐)

通过替换原通知的PendingIntent,先指向自定义广播接收器完成追踪,再跳转原目标页面。完美兼容自研、Firebase及可自定义PendingIntent的第三方通知。

步骤1:实现追踪广播接收器

public class NotificationClickTrackerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 1. 执行点击追踪逻辑
        String source = intent.getStringExtra("NOTIFICATION_SOURCE");
        int notificationId = intent.getIntExtra("NOTIFICATION_ID", -1);
        // 这里调用你的数据分析上报接口
        trackNotificationClick(source, notificationId);

        // 2. 跳转原目标页面
        ComponentName targetComponent = intent.getParcelableExtra("TARGET_COMPONENT");
        Bundle targetExtras = intent.getBundleExtra("TARGET_EXTRAS");
        
        Intent targetIntent = new Intent();
        targetIntent.setComponent(targetComponent);
        if (targetExtras != null) {
            targetIntent.putExtras(targetExtras);
        }
        targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(targetIntent);
    }

    private void trackNotificationClick(String source, int notificationId) {
        // 示例:上报通知来源、ID等信息
        // Analytics.logEvent("notification_click", Map.of("source", source, "id", notificationId));
    }
}

步骤2:在Manifest注册接收器

<receiver android:name=".NotificationClickTrackerReceiver" />

步骤3:修改通知创建逻辑

替换原PendingIntent为指向接收器的实例,同时携带追踪标识和原页面信息:

// 原目标页面Intent
Intent originalIntent = new Intent(context, TargetActivity.class);
originalIntent.putExtra("key", "value");

// 构建追踪Intent
Intent trackerIntent = new Intent(context, NotificationClickTrackerReceiver.class);
trackerIntent.putExtra("NOTIFICATION_SOURCE", "firebase"); // 区分来源:firebase/local/thirdparty_xxx
trackerIntent.putExtra("NOTIFICATION_ID", notificationId);
trackerIntent.putExtra("TARGET_COMPONENT", originalIntent.getComponent());
trackerIntent.putExtra("TARGET_EXTRAS", originalIntent.getExtras());

// 创建追踪用PendingIntent
PendingIntent trackerPi = PendingIntent.getBroadcast(
    context,
    notificationId,
    trackerIntent,
    PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

// 设置给通知
Notification.Builder builder = new Notification.Builder(context, "channel_id")
    .setContentTitle("通知标题")
    .setContentText("通知内容")
    .setSmallIcon(R.drawable.ic_notification)
    .setContentIntent(trackerPi);

方案2:透明中间页(兼容厂商广播限制)

如果部分厂商拦截后台广播,可改用透明Activity作为中间层,逻辑与广播方案一致:

步骤1:创建透明Activity

public class NotificationTrackerActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 执行追踪逻辑
        String source = getIntent().getStringExtra("NOTIFICATION_SOURCE");
        int notificationId = getIntent().getIntExtra("NOTIFICATION_ID", -1);
        trackNotificationClick(source, notificationId);

        // 跳转原页面
        ComponentName targetComponent = getIntent().getParcelableExtra("TARGET_COMPONENT");
        Bundle targetExtras = getIntent().getBundleExtra("TARGET_EXTRAS");
        
        Intent targetIntent = new Intent();
        targetIntent.setComponent(targetComponent);
        if (targetExtras != null) {
            targetIntent.putExtras(targetExtras);
        }
        startActivity(targetIntent);
        finish(); // 立即销毁中间页
    }

    private void trackNotificationClick(String source, int notificationId) {
        // 同广播方案的追踪逻辑
    }
}

步骤2:配置透明主题

在styles.xml添加:

<style name="TransparentActivityTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@null</item>
</style>

Manifest注册Activity时指定主题:

<activity
    android:name=".NotificationTrackerActivity"
    android:theme="@style/TransparentActivityTheme"
    android:exported="false" />

步骤3:修改PendingIntent指向

将方案1中的广播Intent替换为中间页Intent即可。

方案3:统一通知创建入口(减少改动量)

如果App内通知创建逻辑分散,先封装统一的NotificationHelper工具类,所有通知都通过该类构建,只需修改一次逻辑:

public class NotificationHelper {
    public static Notification.Builder buildTrackableNotification(
        Context context,
        String source,
        int notificationId,
        Intent targetIntent,
        String title,
        String content
    ) {
        // 这里复用方案1或方案2的PendingIntent构建逻辑
        Intent trackerIntent = new Intent(context, NotificationClickTrackerReceiver.class);
        trackerIntent.putExtra("NOTIFICATION_SOURCE", source);
        trackerIntent.putExtra("NOTIFICATION_ID", notificationId);
        trackerIntent.putExtra("TARGET_COMPONENT", targetIntent.getComponent());
        trackerIntent.putExtra("TARGET_EXTRAS", targetIntent.getExtras());

        PendingIntent trackerPi = PendingIntent.getBroadcast(
            context,
            notificationId,
            trackerIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        return new Notification.Builder(context, "channel_id")
            .setContentTitle(title)
            .setContentText(content)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentIntent(trackerPi);
    }
}

第三方通知特殊处理

若第三方SDK不允许自定义PendingIntent(如系统直接展示通知),可通过NotificationListenerService监听全局通知点击:

public class NotificationListener extends NotificationListenerService {
    @Override
    public void onNotificationClicked(StatusBarNotification sbn) {
        super.onNotificationClicked(sbn);
        String packageName = sbn.getPackageName();
        if (!packageName.equals(getPackageName())) {
            // 上报第三方通知点击,用包名区分来源
            trackNotificationClick("thirdparty_" + packageName, sbn.getId());
        }
    }
}

需在Manifest注册并引导用户授予通知监听权限。

内容的提问来源于stack exchange,提问作者Felipezad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:55:01