codemonkeys-ru / gcm-message
Google Cloud Messaging (GCM) PHP 服务器库
v0.5
2017-01-13 23:24 UTC
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: 5.7.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2024-09-14 15:28:26 UTC
README
一个用于通过 Google Cloud Messaging 向设备发送消息的 PHP 库
查看: https://developer.android.com.cn/guide/google/gcm/index.html
示例用法
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; } }
也提供间接消息 API
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; } }
关于 cURL SSL 验证对等选项的说明
库默认关闭了 CURLOPT_SSL_VERIFYPEER,但您可以通过将第三个参数传递到 Sender 类的构造函数中启用它。
您需要 下载 根证书并将它们添加到您的项目目录中的某个位置。然后按照以下方式构建 Sender 对象
use \CodeMonkeysRu\GCM; $sender = new GCM\Sender("YOUR GOOGLE API KEY", false, "/path/to/cacert.crt");
变更日志
- v0.5 - 添加了对 "priority" 标志的支持 (#16)
- v0.4 - 代码清理,停止支持 PHP5.5
- v0.3 - 添加了 "content-available" 功能 (#11)
- v0.2 - 添加了通知
- v0.1 - 首次发布
受 MIT 许可证许可。