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

React Native iOS应用如何通过Pusher简便实现远程推送通知?

当然有啦!既然你的后端已经集成了Pusher,那在React Native iOS应用里实现远程推送其实有个很顺畅的简便方案——用Pusher官方的Beams服务(之前叫Pusher Push Notifications),和你现有Pusher生态无缝衔接,不用额外折腾其他推送服务。下面给你一步步拆解:

核心思路:复用Pusher Beams实现推送

Pusher Beams是Pusher专门做远程推送的子服务,和你现有的Pusher后端集成非常友好,只需要在前端接入SDK、配置iOS推送权限,后端就能直接通过Pusher的接口发送推送了。

1. 给React Native项目安装Pusher Beams SDK

先在项目里安装依赖:

# npm 方式
npm install @pusher/push-notifications-react-native --save

# yarn 方式
yarn add @pusher/push-notifications-react-native

然后切换到iOS目录安装pod依赖:

cd ios && pod install && cd ..

2. 配置iOS端的推送基础环境

这一步是iOS推送的必备操作,和Pusher结合起来也很简单:

  • 先在Apple Developer后台为你的应用创建APNs推送证书(区分开发和生产环境)
  • 登录Pusher控制台,找到你的Beams实例,把刚才的APNs证书上传进去,同时确保配置的Bundle ID和你的iOS应用完全匹配
  • 在iOS项目的AppDelegate里配置推送注册和Pusher Beams初始化:
    如果你用的是Objective-C,代码大概是这样:
    #import <PushNotifications/PushNotifications.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // 向用户请求推送权限
      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
      [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
          [application registerForRemoteNotifications];
        }
      }];
    
      // 初始化Pusher Beams,替换成你的实例ID
      [[PushNotifications shared] startWithInstanceId:@"YOUR_PUSHER_BEAMS_INSTANCE_ID"];
      [[PushNotifications shared] registerForRemoteNotifications];
    
      // 保留原有的React Native初始化代码
      return YES;
    }
    
    // 把设备的APNs Token传给Pusher Beams
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
      [[PushNotifications shared] registerDeviceToken:deviceToken];
    }
    
    Swift版本的配置逻辑类似,记得要配置好桥接文件哦。

3. 在React Native代码里订阅兴趣组(Interests)

Pusher Beams用“兴趣组”来管理推送目标,你可以在用户登录、进入特定场景时,让用户订阅对应的兴趣(比如用户ID、话题ID):

import PushNotifications from '@pusher/push-notifications-react-native';

// 比如用户登录后,订阅以用户ID命名的兴趣组
const subscribeToUserPush = async (userId) => {
  try {
    await PushNotifications.subscribe(`user-${userId}`);
    console.log('已成功订阅用户专属推送');
  } catch (error) {
    console.error('订阅推送失败:', error);
  }
};

4. 后端利用现有Pusher集成发送推送

因为你后端已经有Pusher了,只需要引入Pusher Beams的服务端SDK,就能直接给指定兴趣组发推送。比如用Node.js的话:

const PushNotifications = require('@pusher/push-notifications-server');

// 初始化Beams客户端,用你的实例ID和密钥
const beamsClient = new PushNotifications({
  instanceId: 'YOUR_PUSHER_BEAMS_INSTANCE_ID',
  secretKey: 'YOUR_PUSHER_BEAMS_SECRET_KEY',
});

// 给订阅了"user-123"兴趣组的设备发iOS推送
const sendPushToUser = async (userId) => {
  try {
    const publishResponse = await beamsClient.publishToInterests([`user-${userId}`], {
      apns: {
        aps: {
          alert: {
            title: '新消息提醒',
            body: '你有一条未读消息,快来看看吧!',
          },
          badge: 1, // 角标计数
          sound: 'default'
        },
      },
    });
    console.log('推送发送成功,ID:', publishResponse.publishId);
  } catch (error) {
    console.error('推送发送失败:', error);
  }
};

额外小贴士

  • iOS模拟器收不到远程推送,一定要用真机测试
  • 如果需要处理用户点击推送的逻辑,可以在React Native里添加监听:
    PushNotifications.onNotificationOpened((notification) => {
      console.log('用户点击了推送:', notification);
      // 这里可以写跳转逻辑,比如打开对应的消息页面
    });
    
  • 要是遇到推送收不到的情况,先检查APNs证书是否有效、Pusher Beams配置是否正确,还有设备是否成功订阅了目标兴趣组

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:35:47