issetbv/push-notification

PushNotification 是一个推送消息抽象,允许您轻松地向移动设备发送推送通知。目前我们支持 Apple、Android 和 Windows(实验性)

1.2.0 2017-04-05 12:50 UTC

README

Build Status Coverage Status Software License PHPStan

PushNotification 是一个推送消息抽象,允许您轻松地向移动设备发送推送通知。目前我们支持 Apple、Android 和 Windows(实验性)

为什么还需要另一个通知包

  • 市面上有许多支持移动推送通知的包,但大多数不支持统一的 API。
  • 一些实现缺乏灵活的日志记录功能
  • 大多数实现不支持批量/队列
  • 大多数实现没有考虑到当向 Apple 发送批量消息时,如果其中一条消息失败,整个批次将被丢弃。这意味着如果您有一个包含 50 条消息的队列,而第 3 条消息失败,将有 47 条消息被丢弃。这让我们非常沮丧,因此我们构建了一个简单而有效的回退机制,可以在失败消息之后从第一条消息重新启动批次。

目标

  • 有一个通用的 API,用于发送与设备无关的推送通知。
  • 输出一致。
  • 与其他包/框架良好集成。
  • 有一个灵活的日志记录机制。
  • 有内置的队列机制。
  • 在处理批量(特别是发送到 Apple)时,有内置的队列恢复功能。

先决条件

安装

通过 Composer

  composer require issetbv/push-notification

集成

目前我们仅支持 Symfony,因为我们使用的是它,但您可以自由创建自己的集成,并将包含链接的 PR 发送给我们

支持的设备

  • Android
  • Apple
  • Windows(实验性)

文档

大部分文档存储在本包的 docs/index.md 文件中

阅读文档

如果您只想发送消息而不使用 NotificationCenter,以下是一些简单的 TL;DR 示例

简单 Android 示例

要向 Android 设备发送推送通知,我们首先需要设置一个连接。连接需要一个 名称API URL 和您的 API 密钥。最后,我们需要我们想要发送消息的设备的设备令牌。

    use IssetBV\PushNotification\Type\Android\AndroidConnection;
    use IssetBV\PushNotification\Type\Android\Message\AndroidMessage;
    
    $connection = new AndroidConnection(
        $name,    // 'android'
        $api_url, // 'https://fcm.googleapis.com/fcm/send'
        $api_key, // 'super-secret-api-key
    );
    
    $message = new AndroidMessage('my-device-token');
    $message->addToPayload('notification', ['title' => 'Test android']);
    
    $response = $connection->sendAndReceive($message);
    
    echo $response->isSuccess(); // should be true

简单 Apple 示例

要向 Apple 设备发送推送通知,我们首先需要设置一个连接。连接需要一个 名称API URLpem 文件 的位置以及 pem 文件的密码(如果有的话)。最后,我们需要我们想要发送消息的设备的设备标识符。

    use IssetBV\PushNotification\Type\Apple\AppleConnection;
    use IssetBV\PushNotification\Type\Apple\Message\AppleMessageAps;
    
    $connection = new AppleConnection(
        $name,      // 'apple'
        $api_url,   // 'ssl://gateway.push.apple.com:2195'  
        $pemFile,   // __DIR__ '/pemfile.pem'
        $passPhrase // 'super-secret-passphrase'
    );
    
    $appleMessage = new AppleMessageAps('my-device-identifier');
    // see notes below as to why we don't use ->addToPayload()
    $appleMessage->getAps()->setAlert('Test apple');    
    
    $response = $connection->sendAndReceive($appleMessage);
    
    echo $response->isSuccess(); // should be true

在向 Apple 发送消息时,有效负载可以有一个名为 aps 的特定键,我们可以指定它应该显示屏幕上的通知。有关消息格式的更多信息,请参阅 官方 Apple 文档

简单 Windows 示例

要向 Windows 设备发送推送通知,我们首先需要设置一个连接。与 Android 或 Apple 不同,Windows 消息需要特定设备的 URI 来传递消息。

    use IssetBV\PushNotification\Type\Windows\Message\WindowsMessage;
    use IssetBV\PushNotification\Type\Windows\WindowsConnection;
    
    $connection = new WindowsConnection('windows');
    
    $windowsMessage = new WindowsMessage('https://cloud.notify.windows.com/?token=AQE%bU%2fSjZOCvRjjpILow%3d%3d');
    $windowsMessage->addToPayload('wp:Text1', 'Test Windows');
    
    $response = $connection->sendAndReceive($windowsMessage);
    
    echo $response->isSuccess(); // should be true