alymosul/exponent-server-sdk-php

使用PHP处理Expo推送通知的服务端库

1.3.1 2023-05-12 18:31 UTC

This package is auto-updated.

Last update: 2024-09-12 21:13:36 UTC


README

使用PHP处理Expo推送通知的服务端库

Latest Stable Version License Total Downloads

用法

  • 在您的项目中需要此包
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仪表板中设置了增强安全(如此处所述),您需要在每个推送请求中附加一个授权令牌

    // ...
    
    // 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驱动器