ctwillie/expo-server-sdk-php

使用 PHP 与 Expo 服务器端库

v2.1 2023-12-02 00:34 UTC

This package is auto-updated.

Last update: 2024-08-31 00:36:14 UTC


README

使用 PHP 与 Expo 服务器端库

如果您在这个仓库中的代码遇到任何问题,请随时 提交问题 或创建 PR!

目录

测试

您可以通过 composer 运行测试套件

composer test

安装

您可以通过 composer 安装此包

composer require ctwillie/expo-server-sdk-php

用例

此包主要针对两个主要用例编写。

  1. 向一个或多个接收者发送推送通知消息,然后您就完成了!这是最明显的用例。
  2. 并且频道订阅,用于将一个或多个令牌订阅到频道,然后向订阅该频道的所有令牌发送推送通知。订阅将在令牌从频道取消订阅之前持续存在。也许在最终用户请求时取消订阅。

在决定哪个是您后端最佳用例时请记住这一点。

消息组成

使用来自 Expo 文档 的选项编写要发送的推送通知消息。

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();

发送推送通知

编写消息并发送给一个或多个接收者。

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();

频道订阅

将令牌订阅到频道,然后向该频道推送通知消息。订阅将保存在内部本地文件中,因此您不必担心这一点。在任何时候取消令牌的频道订阅以停止向该接收者发送消息。

⚠️ 如果您正在运行多个应用程序服务器:请非常小心!频道订阅保存在内部本地文件中。订阅不会在多个服务器之间共享。

/**
 * 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 响应

获取从 Expo 服务器成功响应返回的数据。

$response = $expo->send($message)->to($recipients)->push();

$data = $response->getData();

处理未注册设备

Expo 为处理在 Expo 响应中具有 DeviceNotRegistered 错误的令牌提供宏。在发送消息之前,您可以注册回调来处理这些未注册的令牌。

您只需注册一次处理器,因为它将应用于所有 Expo 实例。

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

检索推送回执

使用来自 Expo 服务器的票据 ID 检索推送回执。

$ticketIds = [
    'xxxx-xxxx-xxxx-xxxx',
    'yyyy-yyyy-yyyy-yyyy'
];

$response = $expo->getReceipts($ticketIds);
$data = $response->getData();

变更日志

请参阅 变更日志 了解最近更改的详细信息。

贡献

请参阅 贡献 了解详细信息。

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。

鸣谢