scary-layer / exponent-server-sdk-php
使用 PHP 处理 Expo 推送通知的服务端库
1.3.0
2021-06-22 13:15 UTC
Requires
- php: >=7.0
- ext-curl: *
- ext-json: *
This package is not auto-updated.
Last update: 2024-09-25 20:56:50 UTC
README
使用 PHP 处理 Expo 推送通知的服务端库
使用方法
- 在项目中引入此包
composer require alymosul/exponent-server-sdk-php
- 在 PHP 文件中
require_once __DIR__.'/vendor/autoload.php'; $channelName = 'news'; $recipient= 'ExponentPushToken[unique]'; // You can quickly bootup an expo instance $expo = \ExponentPhpSDK\Expo::normalSetup(); // Subscribe the recipient to the server $expo->subscribe($channelName, $recipient); // Build the notification data $notification = ['body' => 'Hello World!']; // Notify an interest with a notification $expo->notify([$channelName], $notification);
可以通过提供 JSON 对象来添加数据到通知中。例如
// Build the notification data $notification = ['body' => 'Hello World!', 'data'=> json_encode(array('someData' => 'goes here'))];
频道名称
您可以使用频道将通知发送给单个用户或一组用户
单个接收者
为了针对单个接收者(并避免发送给错误的接收者),请使用每个用户特定的频道名称
$channelName = 'user_528491'; $recipient = 'ExponentPushToken[unique]'; // … // Subscribe the recipient to the server $expo->subscribe($channelName, $recipient); // … // Notify an interest with a notification, only one recipient will receive it $expo->notify([$channelName], $notification);
多个接收者
声明一个将在接收者之间共享的频道名称
$channelName = 'group_4815'; $recipient1 = 'ExponentPushToken[unique1]'; $recipient2 = 'ExponentPushToken[unique2]'; // … // Subscribe the recipients to the server $expo->subscribe($channelName, $recipient1); $expo->subscribe($channelName, $recipient2); // … // Notify an interest with a notification, the 2 recipients will receive it $expo->notify([$channelName], $notification);
// Build the notification data $notification = ['body' => 'Hello World!', 'data'=> json_encode(array('someData' => 'goes here'))];
额外安全措施
如果您在 Expo Dashboard 中设置了增强安全(如此处所述),则需要将授权令牌附加到每个推送请求中
// ... // Bootup an expo instance $expo = \ExponentPhpSDK\Expo::normalSetup(); // Fetch your access token from where you stored it $accessToken = 'your_expo_access_token'; // The access token will be attached to every push request you make hereafter $expo->setAccessToken($accessToken); // Notify an interest with a notification $expo->notify([$channelName], $notification);
待办事项
- 需要创建测试
Laravel 驱动
- 为 Laravel 应用程序构建了一个可用的 Expo 通知驱动程序,您可以在此处找到它。