sunaoKa / push-notifications-php
移动设备的推送通知/消息
1.0.6
2024-04-19 06:06 UTC
Requires
- php: ^5.5 || ^7.0 || ^8.0
- ext-json: *
- ext-openssl: *
- google/apiclient: ^2.8
- guzzlehttp/guzzle: ^6.0 || ^7.0
- vlucas/valitron: ^1.4
Requires (Dev)
- phpunit/phpunit: ^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
README
支持的协议
安装
composer require sunaoka/push-notifications-php
基本用法
例如,在APNs的情况下,可以使用基于令牌的连接Token Based。
<?php use Sunaoka\PushNotifications\Drivers\APNs; use Sunaoka\PushNotifications\Pusher; $payload = [ 'aps' => [ 'alert' => [ 'title' => 'Game Request', 'body' => 'Bob wants to play poker', ], ], ]; $options = new APNs\Token\Option(); $options->payload = $payload; $options->authKey = '/path/to/key.p8'; $options->keyId = 'ABCDE12345'; $options->teamId = 'ABCDE12345'; $options->topic = 'com.example.app'; $driver = new APNs\Token($options); $pusher = new Pusher(); $feedback = $pusher->to('Device token')->send($driver); $result = $feedback->isSuccess('Device token'); if (! $result) { echo $feedback->failure('Device token'); // BadDeviceToken }
如何指定选项
有两种方式可以指定选项。
$options = new APNs\Token\Option(); $options->payload = $payload; $options->authKey = '/path/to/key.p8'; $options->keyId = 'ABCDE12345'; $options->teamId = 'ABCDE12345'; $options->topic = 'com.example.app';
或
$options = new APNs\Token\Option([ 'payload' => $payload, 'authKey' => '/path/to/key.p8', 'keyId' => 'ABCDE12345', 'teamId' => 'ABCDE12345', 'topic' => 'com.example.app', ]);
多播消息
在Pusher::to()
中指定设备令牌数组。然后,您可以将其分发到多个设备。
$pusher = new Pusher(); $feedback = $pusher->to([ 'Device token 1', 'Device token 2', 'Device token 3', ])->send($driver);
如何在生产环境和开发环境之间切换(仅限APNs)
这在使用Pusher
实例时作为参数指定。
// Development environment (default) $pusher = new Pusher(false);
// Production environment $pusher = new Pusher(true);
反馈
Pusher::send()
的返回值是一个Feedback
对象。
通过Feedback
对象,您可以确定通知是否成功或失败。
$pusher = new Pusher(); $feedback = $pusher->to('Device token')->send($driver); $result = $feedback->isSuccess('Device token'); if ($result) { echo $feedback->success('Device token'); // 01234567-0123-0123-0123-01234567890A } else { echo $feedback->failure('Device token'); // BadDeviceToken }
HTTP请求选项
您可以将Guzzle请求选项作为驱动器选项指定。
$options = new APNs\Token\Option(); $options->httpOptions = [ 'connect_timeout' => 3.14 'timeout' => 3.14, 'debug' => true, ];
更多示例
更多示例可以在示例目录中找到。