luniumall / expo-server-sdk-php
使用PHP与Expo交互的服务端库
v1.2
2021-09-06 15:04 UTC
Requires
- php: >=7.3
- guzzlehttp/guzzle: ^7.3
Requires (Dev)
- phpunit/phpunit: ^9.3
This package is not auto-updated.
Last update: 2024-09-27 05:39:31 UTC
README
使用PHP与Expo交互的服务端库。
如果您在此存储库中的代码有任何问题,请提交问题或制作PR!
测试
您可以通过composer运行测试套件
composer test
安装
您可以通过composer安装此包
composer require ctwillie/expo-server-sdk-php
用例
此包主要针对以下两个用例编写。
- 向一个或多个接收者发送推送通知消息,然后完成任务!最明显的用例。
- 频道订阅,用于将一个或多个令牌订阅到频道,然后向订阅该频道的所有令牌发送推送通知。订阅将保持到令牌从频道取消订阅为止。也许在最终用户请求取消订阅。
在决定后端最佳用例时请记住这一点。
编写消息
使用来自Expo文档的选项编写要发送的推送通知消息。
$message = (new ExpoMessage()) ->setTitle('Message Title') ->setSubtitle('Message sub title') ->setBody('The notification message body') ->setChannelId('default') ->setBadge(0) ->playSound();
发送推送通知
编写消息然后发送到一个或多个接收者。
$expo = new Expo(); // composed message, see above $message; $recipients = [ 'ExponentPushToken[xxxx-xxxx-xxxx]', 'ExponentPushToken[yyyy-yyyy-yyyy]' ]; $expo->send($message)->to($recipients)->push();
频道订阅
将令牌订阅到频道,然后向该频道推送通知消息。订阅将保存在内部本地文件中,因此您无需自己担心此问题。在任何时候从频道取消令牌的订阅以停止向该接收者发送消息。
⚠️ 如果您正在运行多个应用服务器:请非常小心!频道订阅存储在内部本地文件中。订阅不会在多个服务器之间共享。未来将提供数据库驱动程序来处理此用例。
/** * Specify the file driver to persist subscriptions internally. * More drivers coming soon, (database, redis, custom local file) */ $expo = Expo::driver('file'); // composed message, see above $message; $recipients = [ 'ExponentPushToken[xxxx-xxxx-xxxx]', 'ExponentPushToken[yyyy-yyyy-yyyy]' ]; // name your channel anything you'd like $channel = 'news-letter'; // the channel will be created automatically if it doesn't already exist $expo->subscribe($channel, $recipients); $expo->send($message)->toChannel($channel)->push(); // you can unsubscribe one or more recipients from a channel. $expo->unsubscribe($channel, $recipients);
Expo响应
获取来自Expo服务器的成功响应返回的数据。
$response = $expo->send($message)->to($recipients)->push(); $data = $response->getData();
检索推送回执
使用来自Expo服务器的票据ID检索推送回执。
$ticketIds = [ 'xxxx-xxxx-xxxx-xxxx', 'yyyy-yyyy-yyyy-yyyy' ]; $response = $expo->getReceipts($ticketIds); $data = $response->getData();
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。