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

如何在Android应用中获取OneSignal推送的WordPress文章数据及ID以跳转对应Activity?

刚好之前做过WordPress + OneSignal + Android的集成,针对你这两个需求,我给你拆解下具体的实现步骤,分两部分来搞定:

一、在Android应用中获取OneSignal推送的WordPress文章数据

要实现这个,核心是在WordPress推送通知时,把文章的关键数据塞进OneSignal的自定义附加字段,然后Android端接收推送时读取这些数据。

第一步:WordPress端配置,给推送添加文章数据

你可以通过WordPress的钩子,在OneSignal发送通知时自动附加文章的ID、标题、摘要等信息。在主题的functions.php里添加这段代码:

add_filter('onesignal_send_notification', 'attach_post_data_to_notification', 10, 4);
function attach_post_data_to_notification($fields, $new_status, $old_status, $post) {
    // 只对新发布的文章生效(避免草稿、更新时重复触发)
    if ($new_status === 'publish' && $old_status !== 'publish') {
        // 把需要的文章数据塞进OneSignal的data字段
        $fields['data'] = array(
            'post_id' => $post->ID,
            'post_title' => $post->post_title,
            'post_excerpt' => wp_trim_words($post->post_content, 20), // 截取摘要
            'post_link' => get_permalink($post->ID)
            // 还可以加其他字段,比如分类ID、缩略图URL等
        );
    }
    return $fields;
}

这段代码会在文章发布时,自动把文章的核心数据附加到OneSignal推送的data参数里。

第二步:Android端接收推送中的文章数据

在Android应用里,通过OneSignal的回调监听推送事件,就能拿到这些自定义数据。首先确保你已经正确初始化OneSignal(在Application的onCreate里):

@Override
public void onCreate() {
    super.onCreate();
    // 初始化OneSignal
    OneSignal.initWithContext(this);
    OneSignal.setAppId("你的OneSignal App ID");
}

然后设置推送打开的回调,在里面读取文章数据:

OneSignal.setNotificationOpenedHandler(new NotificationOpenedHandler() {
    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        // 获取推送里的自定义附加数据
        JSONObject additionalData = result.getNotification().getAdditionalData();
        if (additionalData != null) {
            // 提取文章数据
            String postId = additionalData.optString("post_id");
            String postTitle = additionalData.optString("post_title");
            String postExcerpt = additionalData.optString("post_excerpt");
            
            // 这里就可以用这些数据做你需要的操作了,比如缓存、展示等
        }
    }
});

如果需要在推送到达时就获取数据(不用等用户打开),可以设置NotificationReceivedHandler:

OneSignal.setNotificationReceivedHandler(new NotificationReceivedHandler() {
    @Override
    public void notificationReceived(OSNotification notification) {
        JSONObject additionalData = notification.getAdditionalData();
        if (additionalData != null) {
            // 同样可以提取文章数据
        }
    }
});
二、打开推送时根据Post ID启动对应Activity

这部分其实是上面步骤的延伸,拿到post_id后,根据你的业务逻辑判断要启动哪个Activity即可。

在刚才的notificationOpened回调里,添加启动Activity的逻辑:

OneSignal.setNotificationOpenedHandler(new NotificationOpenedHandler() {
    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        JSONObject additionalData = result.getNotification().getAdditionalData();
        if (additionalData != null) {
            String postId = additionalData.optString("post_id", "");
            if (!postId.isEmpty()) {
                Intent targetIntent;
                
                // 这里根据你的业务规则判断启动哪个Activity
                // 示例:假设post_id为100以内的启动SpecialActivity,其他启动NormalActivity
                if (Integer.parseInt(postId) < 100) {
                    targetIntent = new Intent(getApplicationContext(), SpecialArticleActivity.class);
                } else {
                    targetIntent = new Intent(getApplicationContext(), NormalArticleActivity.class);
                }
                
                // 把post_id传递给目标Activity,方便后续加载文章内容
                targetIntent.putExtra("EXTRA_POST_ID", postId);
                
                // 必须设置FLAG_ACTIVITY_NEW_TASK,因为回调不在Activity上下文里
                targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                
                // 启动Activity
                startActivity(targetIntent);
            }
        }
    }
});

一些注意事项

  • 确保WordPress的OneSignal插件版本是最新的,避免钩子不生效的问题
  • Android端要在AndroidManifest.xml里正确注册需要启动的Activity
  • 如果你的判断逻辑比较复杂(比如需要根据文章分类判断),可以考虑在WordPress端把分类ID也附加到data字段里,这样Android端直接读取判断更方便

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:36:04