ctwillie/expo-server-sdk-php
Composer 安装命令:
composer require ctwillie/expo-server-sdk-php
包简介
Server-side library for working with Expo using PHP
README 文档
README
Server-side library for working with Expo using PHP.
If you have any problems with the code in this repository, feel free to open an issue or make a PR!
Table of Contents
Testing
You can run the test suite via composer:
composer test
Installation
You can install the package via composer:
composer require ctwillie/expo-server-sdk-php
Use Cases
This package was written with two main use cases in mind.
- Sending push notification messages to one or more recipients, then you're done! The most obvious use case.
- And channel subscriptions, used to subscribe one or more tokens to a channel, then send push notifications to all tokens subscribed to that channel. Subscriptions are persisted until a token unsubscribes from a channel. Maybe unsubscribing upon the end users request.
Keep this in mind as you decide which is the best use case for your back end.
Composing a message
Compose a push notification message to send using options from the Expo docs.
use ExpoSDK\ExpoMessage; /** * Create messages fluently and/or pass attributes to the constructor */ $message = (new ExpoMessage([ 'title' => 'initial title', 'body' => 'initial body', ])) ->setTitle('This title overrides initial title') ->setBody('This notification body overrides initial body') ->setData(['id' => 1]) ->setChannelId('default') ->setBadge(0) ->playSound();
Sending a push notification
Compose a message then send to one or more recipients.
use ExpoSDK\Expo; use ExpoSDK\ExpoMessage; /** * Composed messages, see above * Can be an array of arrays, ExpoMessage instances will be made internally */ $messages = [ [ 'title' => 'Test notification', 'to' => 'ExponentPushToken[xxxx-xxxx-xxxx]', ], new ExpoMessage([ 'title' => 'Notification for default recipients', 'body' => 'Because "to" property is not defined', ]), ]; /** * These recipients are used when ExpoMessage does not have "to" set */ $defaultRecipients = [ 'ExponentPushToken[xxxx-xxxx-xxxx]', 'ExponentPushToken[yyyy-yyyy-yyyy]' ]; (new Expo)->send($messages)->to($defaultRecipients)->push();
Channel subscriptions
Subscribe tokens to a channel, then push notification messages to that channel. Subscriptions are persisted internally in a local file so you don't have to worry about this yourself. Unsubscribe the token from the channel at any time to stop messages to that recipient.
⚠️ If you are are running multiple app servers: Be very careful here! Channel subscriptions are stored in an internal local file. Subscriptions will not be shared across multiple servers.
/** * Specify the file driver to persist subscriptions internally. */ use ExpoSDK\Expo; $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 responses
Get the data returned from successful responses from the Expo server.
$response = $expo->send($message)->to($recipients)->push(); $data = $response->getData();
Handling unregistered devices
Expo provides a macro for handling tokens that have DeviceNotRegistered error in the Expo response. You can register a callback before sending your messages to handle these unregistered tokens.
You only need to register the handler once as it will be applied to all Expo instances.
use ExpoSDK\Expo; Expo::addDevicesNotRegisteredHandler(function ($tokens) { // this callback is called once and receives an array of unregistered tokens }); $expo1 = new Expo(); $expo1->send(...)->push(); // will call your callback $expo2 = new Expo(); $expo2->send(...)->push(); // will also call your callback
Retrieving push receipts
Retrieve the push receipts using the ticket ids from the Expo server.
$ticketIds = [ 'xxxx-xxxx-xxxx-xxxx', 'yyyy-yyyy-yyyy-yyyy' ]; $response = $expo->getReceipts($ticketIds); $data = $response->getData();
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
Credits
ctwillie/expo-server-sdk-php 适用场景与选型建议
ctwillie/expo-server-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 815.98k 次下载、GitHub Stars 达 65, 最近一次更新时间为 2021 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「expo」 「expo-server-sdk-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ctwillie/expo-server-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ctwillie/expo-server-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ctwillie/expo-server-sdk-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expo Push Notifications Driver for Laravel Notifications, PHP 7.4
Server-side library for working with the Expo push notification service using PHP 8.2+
Yii2 extension for working with Expo push notifications
Send push notifications with Expo
Server-side library for working with Expo using PHP
Server-side library for working with Expo push notifications using PHP
统计信息
- 总下载量: 815.98k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 67
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-27