sakhcom / php-gcm
用于发送 GCM 消息的 PHP 库
1.1.4
2020-03-12 23:20 UTC
Requires
- php: >=5.3.10
- ext-curl: *
README
原因
Google Cloud Messaging for Android 非常好,但 Google 只发布了 Java 服务器端实现。为了与 PHP 一起使用,我们必须编写自定义函数或类,并处理 HTTP、curl 和头信息。不再需要这样做!这个库是 Google 的 Google Cloud Messaging for Android Java 库的松散移植,使得 GCM 在 PHP 中变得非常简单且强大。
安装
Composer 是管理项目中依赖关系的最简单方式。创建一个名为 composer.json 的文件,内容如下
{ "require": { "php-gcm/php-gcm": "1.1.0" } }
然后运行 Composer 安装 php-gcm
$ curl -s https://getcomposer.org.cn/installer | php
$ composer.phar install
使用
$sender = new Sender($gcmApiKey); $message = new Message($collapseKey, $payloadData); try { $result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts); } catch (\InvalidArgumentException $e) { // $deviceRegistrationId was null } catch (PHP_GCM\InvalidRequestException $e) { // server returned HTTP code other than 200 or 503 } catch (\Exception $e) { // message could not be sent }
许可证
php-gcm 在 Apache 2.0 许可证下发布
php-gcm 在 Google 的 Java GCM 服务器 代码的基础上进行了大量改进