跳到内容

Expo SDK 基础集成指南

本节包含有关如何将 Pushwoosh Expo SDK 集成到您的应用程序中的信息。

先决条件

Anchor link to

要将 Pushwoosh Expo SDK 集成到您的应用中,您需要以下内容:

集成步骤

Anchor link to

1. 安装插件

Anchor link to

使用 Expo CLI 安装 Pushwoosh Expo 插件

Terminal window
expo install pushwoosh-expo-plugin

安装 Pushwoosh React Native SDK

Terminal window
npm install pushwoosh-react-native-plugin --save

2. 设置插件属性

Anchor link to

将插件及其必要属性添加到插件数组的前面:

app.json/app.config.js
{
"expo": {
"plugins": [
[
"pushwoosh-expo-plugin",
{
"mode": "development",
"ios": {
"PW_API_TOKEN": "__YOUR_DEVICE_API_TOKEN__"
},
"android": {
"apiToken": "__YOUR_DEVICE_API_TOKEN__"
}
}
]
]
}
}

其中:

  • mode 用于配置 APNs 环境授权。“Development” 或 “production” 值为可用值。
  • PW_API_TOKENapiToken 是您的 Pushwoosh Device API Token

3. 初始化 Pushwoosh

Anchor link to

在您应用程序的根组件中:

  • 导入 pushwoosh-react-native-plugin 插件。
  • 初始化 Pushwoosh SDK。
  • 在您的初始化逻辑中调用 register() 以注册推送通知。
index.tsx
import Pushwoosh from 'pushwoosh-react-native-plugin';
Pushwoosh.init({
"pw_appid": "__YOUR_APP_ID__"
});
Pushwoosh.register();

其中:

  • __YOUR_APP_ID__ 是来自 Pushwoosh 控制面板的应用程序代码。

4. Android 原生设置

Anchor link to

添加 Firebase 配置文件:

  1. 将您的 google-services.json 文件复制到项目的根目录。
  2. googleServicesFile 属性设置为您的 google-services.json 的路径,并指定 package 属性:
app.json/app.config.js
"expo": {
"name": "sample",
"android": {
"package": "com.pushwoosh.sample",
"googleServicesFile": "./google-services.json"
},
"plugins": [
[
"pushwoosh-expo-plugin",
{
"mode": "development",
"ios": {
"PW_API_TOKEN": "__YOUR_DEVICE_API_TOKEN__"
},
"android": {
"apiToken": "__YOUR_DEVICE_API_TOKEN__"
}
}
]
]
}

5. iOS 原生设置

Anchor link to

bundleIdentifier 属性设置到 ios 对象:

app.json/app.config.js
"expo": {
"name": "sample",
"ios": {
"bundleIdentifier": "com.pushwoosh.sample"
},
"plugins": [
[
"pushwoosh-expo-plugin",
{
"mode": "development",
"ios": {
"PW_API_TOKEN": "__YOUR_DEVICE_API_TOKEN__"
},
"android": {
"apiToken": "__YOUR_DEVICE_API_TOKEN__"
}
}
]
]
}

6. 预构建应用

Anchor link to

通过运行 prebuild 为每个平台生成原生代码并配置依赖项:

Terminal window
npx expo prebuild

7. 运行项目

Anchor link to
  1. 构建并运行项目:
Terminal window
npx expo run:android
  1. 前往 Pushwoosh 控制面板并发送一条推送通知
  2. 您应该会在应用中看到该通知。

扩展集成

Anchor link to

至此,您已经集成了 SDK,可以发送和接收推送通知。现在,让我们来探索核心功能。

推送通知事件监听器

Anchor link to

在 Pushwoosh SDK 中有两个事件监听器,用于处理推送通知:

  • onPushReceived 事件在收到推送通知时触发
  • onPushAccepted 事件在用户打开通知时触发

您应该在应用程序启动时初始化 SDK 后立即设置这些事件监听器:

import { DeviceEventEmitter } from 'react-native';
import Pushwoosh from 'pushwoosh-react-native-plugin';
class PushwooshNotificationHandler {
setupPushListeners(): void {
DeviceEventEmitter.addListener("pushReceived", (e) => {
console.warn("Push received: " + JSON.stringify(e));
});
DeviceEventEmitter.addListener("pushOpened", (e) => {
console.warn("Push opened:" + JSON.stringify(e));
});
}
}

用户配置

Anchor link to

通过关注个人用户的行为和偏好,您可以提供个性化内容,从而提高用户满意度和忠诚度。

import Pushwoosh from 'pushwoosh-react-native-plugin';
class Registration {
afterUserLogin(user: User): void {
// 设置用户 ID
Pushwoosh.setUserId(user.getId());
// 设置用户邮箱
Pushwoosh.setEmails(user.getEmailList());
// 将额外的用户信息设置为 Pushwoosh 的标签
Pushwoosh.setTags({
"age": user.getAge(),
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
}

标签是分配给用户或设备的键值对,允许根据偏好或行为等属性进行分段,从而实现有针对性的消息传递。

import Pushwoosh from 'pushwoosh-react-native-plugin';
class UpdateUser {
afterUserUpdateProfile(user: User): void {
// 设置喜爱的类别列表
Pushwoosh.setTags({
"favorite_categories": user.getFavoriteCategoriesList()
});
// 设置支付信息
Pushwoosh.setTags({
"is_subscribed": user.isSubscribed(),
"payment_status": user.getPaymentStatus(),
"billing_address": user.getBillingAddress()
});
}
}

事件是应用内可以跟踪的特定用户操作或发生的事情,用于分析行为并触发相应的消息或操作。

import Pushwoosh from 'pushwoosh-react-native-plugin';
class Registration {
// 跟踪登录事件
afterUserLogin(user: User): void {
Pushwoosh.postEvent("login", {
"name": user.getName(),
"last_login": user.getLastLoginDate()
});
}
// 跟踪购买事件
afterUserPurchase(product: Product): void {
Pushwoosh.postEvent("purchase", {
"product_id": product.getId(),
"product_name": product.getName(),
"price": product.getPrice(),
"quantity": product.getQuantity()
});
}
}

iOS 消息送达跟踪

Anchor link to

您必须向您的项目中添加一个 Notification Service Extension 目标。这对于在 iOS 上实现准确的送达跟踪和富媒体等功能至关重要。

请遵循原生指南的步骤来添加扩展目标并在其中添加必要的 Pushwoosh 代码。

额外的插件属性

Anchor link to
Property
Default
描述
iOS properties
Pushwoosh_LOG_LEVELINFOiOS 的日志级别。可能的值:NONEERRORWARNINFODEBUGNOISE
Android properties
logLevelINFOAndroid 的日志级别。可选值:NONEERRORWARNINFODEBUGNOISE
multiNotificationModetrue如果您只想为用户显示最后一条通知,可以将其更改为 false
icon-Android 自定义通知图标的路径

故障排除

Anchor link to

如果您在集成过程中遇到任何问题,请参阅支持和社区部分。

FCM 注册错误:无法检索令牌。Firebase 是否配置正确?
Anchor link to

请确保您的 Firebase googleServicesFile 属性已在 Expo 配置文件中设置,并且 google-services.json 文件已添加到您项目的根目录:

app.json/app.config.js
"expo": {
"name": "sample",
"android": {
"package": "com.pushwoosh.sample",
"googleServicesFile": "./google-services.json"
},
"plugins": [
[
"pushwoosh-expo-plugin",
{
"mode": "development",
"ios": {
"PW_API_TOKEN": "__YOUR_DEVICE_API_TOKEN__"
},
"android": {
"apiToken": "__YOUR_DEVICE_API_TOKEN__"
}
}
]
]
}
TypeError: Cannot read property ‘init’ of null
Anchor link to

当您尝试在设备上运行应用时,可能会遇到此错误。 要解决此问题,请确保您已完成预构建步骤。它会为每个平台生成原生代码并配置依赖项。

Terminal window
npx expo prebuild