codemonkeys-ru/gcm-message
Composer 安装命令:
composer require codemonkeys-ru/gcm-message
包简介
Google Cloud Messaging (GCM) PHP Server Library
关键字:
README 文档
README
A PHP library for sending messages to devices registered through Google Cloud Messaging
See: http://developer.android.com/guide/google/gcm/index.html
Example usage
use \CodeMonkeysRu\GCM; $sender = new GCM\Sender("YOUR GOOGLE API KEY"); $message = new GCM\Message( array("device_registration_id1", "device_registration_id2"), array("data1" => "123", "data2" => "string") ); $message ->notification(array("title" => "foo", "body" => "bar")) ->setCollapseKey("collapse_key") ->setDelayWhileIdle(true) ->setTtl(123) ->setRestrictedPackageName("com.example.trololo") ->setDryRun(true) ->setPriority(GCM\MessageMessage::PRIORITY_HIGH) ; try { $response = $sender->send($message); if ($response->getNewRegistrationIdsCount() > 0) { $newRegistrationIds = $response->getNewRegistrationIds(); foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){ //Update $oldRegistrationId to $newRegistrationId in DB //TODO } } if ($response->getFailureCount() > 0) { $invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds(); foreach($invalidRegistrationIds as $invalidRegistrationId) { //Remove $invalidRegistrationId from DB //TODO } //Schedule to resend messages to unavailable devices $unavailableIds = $response->getUnavailableRegistrationIds(); //TODO } } catch (GCM\Exception $e) { switch ($e->getCode()) { case GCM\Exception::ILLEGAL_API_KEY: case GCM\Exception::AUTHENTICATION_ERROR: case GCM\Exception::MALFORMED_REQUEST: case GCM\Exception::UNKNOWN_ERROR: case GCM\Exception::MALFORMED_RESPONSE: //Deal with it break; } }
Also indirect message API available
use \CodeMonkeysRu\GCM; $sender = new GCM\Sender("YOUR GOOGLE API KEY"); try { $response = $sender->sendMessage( array("device_registration_id1", "device_registration_id2"), array("data1" => "123", "data2" => "string"), "collapse_key" ); if ($response->getNewRegistrationIdsCount() > 0) { $newRegistrationIds = $response->getNewRegistrationIds(); foreach ($newRegistrationIds as $oldRegistrationId => $newRegistrationId){ //Update $oldRegistrationId to $newRegistrationId in DB //TODO } } if ($response->getFailureCount() > 0) { $invalidRegistrationIds = $GCMresponse->getInvalidRegistrationIds(); foreach($invalidRegistrationIds as $invalidRegistrationId) { //Remove $invalidRegistrationId from DB //TODO } //Schedule to resend messages to unavailable devices $unavailableIds = $response->getUnavailableRegistrationIds(); //TODO } } catch (GCM\Exception $e) { switch ($e->getCode()) { case GCM\Exception::ILLEGAL_API_KEY: case GCM\Exception::AUTHENTICATION_ERROR: case GCM\Exception::MALFORMED_REQUEST: case GCM\Exception::UNKNOWN_ERROR: case GCM\Exception::MALFORMED_RESPONSE: //Deal with it break; } }
Note about cURL SSL verify peer option
Library has turned off CURLOPT_SSL_VERIFYPEER by default, but you can enable it by passing third parameter into constructor of Sender class.
You need to download root certificates and add them somewhere into your project directory. Then construct Sender object like this:
use \CodeMonkeysRu\GCM; $sender = new GCM\Sender("YOUR GOOGLE API KEY", false, "/path/to/cacert.crt");
ChangeLog
- v0.5 - Added support for "priority" flag (#16)
- v0.4 - Code cleanup, PHP5.5 support dropped
- v0.3 - Content-available added (#11)
- v0.2 - Notifications added
- v0.1 - Initial release
Licensed under MIT license.
codemonkeys-ru/gcm-message 适用场景与选型建议
codemonkeys-ru/gcm-message 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 91.07k 次下载、GitHub Stars 达 62, 最近一次更新时间为 2013 年 04 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「android」 「gcm」 「push message」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codemonkeys-ru/gcm-message 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codemonkeys-ru/gcm-message 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codemonkeys-ru/gcm-message 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii 2 Apns and Gcm together
Firebase cloud messaging http v1 php
Standalone PHP library for easy devices notifications push.
Apple Push Notification & Feedback Provider
Laravel package to send push notifications to mobile devices (apns, gcm)
Scrapes app data from Google Play store.
统计信息
- 总下载量: 91.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 63
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-04-22