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

Android中点击OneSignal推送通知如何跳转到指定页面?

OneSignal Android 推送跳转指定页面实现方案

核心实现逻辑是在推送 payload 中携带自定义字段区分通知类型、传递业务参数,客户端接收通知点击事件后根据字段做路由跳转即可,具体步骤如下:

1. 推送发送端配置附加参数

不管是后台手动发送商品优惠通知,还是系统自动触发订单状态通知,发送推送时都需要在 OneSignal 的「附加数据(Additional Data)」中传入两个自定义字段:

  • notify_type:枚举值,商品优惠通知传goods_promotion,订单状态变更通知传order_status_change
  • target_id:业务ID,商品通知传对应商品ID,订单通知传对应订单ID

推送附加数据的JSON示例:

{
  "notify_type": "goods_promotion",
  "target_id": "123456"
}

2. Android 端注册通知点击监听

完成OneSignal基础接入后,在Application的onCreate方法中注册通知打开事件回调,示例代码如下:

// 初始化OneSignal后添加点击事件监听
OneSignal.setNotificationOpenedHandler(result -> {
    // 获取推送附加数据
    JSONObject additionalData = result.getNotification().getAdditionalData();
    if (additionalData == null) {
        // 无附加数据默认跳首页
        Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
        mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(mainIntent);
        return;
    }
    try {
        String notifyType = additionalData.getString("notify_type");
        String targetId = additionalData.getString("target_id");
        Intent targetIntent = null;
        // 根据通知类型跳转对应页面
        if ("goods_promotion".equals(notifyType)) {
            targetIntent = new Intent(getApplicationContext(), GoodsDetailActivity.class);
            targetIntent.putExtra("goods_id", targetId);
        } else if ("order_status_change".equals(notifyType)) {
            targetIntent = new Intent(getApplicationContext(), OrderDetailActivity.class);
            targetIntent.putExtra("order_id", targetId);
        }
        if (targetIntent != null) {
            targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(targetIntent);
        } else {
            // 未知类型跳转首页
            startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }
    } catch (JSONException e) {
        e.printStackTrace();
        // 参数解析异常跳转首页
        startActivity(new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
});

3. 补充场景处理

  • 如果需要在跳转详情页后点击返回能回到APP首页,可以用TaskStackBuilder构建回退栈后再跳转,避免返回直接退出应用
  • 如果项目使用了ARouter等路由框架,直接在回调中拿到notify_type和target_id后调用对应路由跳转即可,逻辑一致
  • 所有参数读取和解析都要加异常捕获,避免非法推送参数导致APP崩溃

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 15:24:05