ThinkPHP框架整合友盟推送DEMO

友盟是中国最大的移动开发者服务平台,为移动开发者提供免费的应用统计分析、社交分享、消息推送、自动更新、在线参数、移动推广效果分析、微社区等app开发和运营解决方案。

本博文讲述如何快速在ThinkPHP框架中集成友盟推送功能:

1、在官网或是在本博文内下载友盟推送PHP_DEMO;

2、将文件夹下的Notification文件夹放入到Application(应用文件目录)下;

3、在每个php文件内为文件根据文件夹的命名添加上合适的命名空间,笔者加的是:

 

  1. //命名空间

  2. namespace Notification;


4、在类文件Sms.class.php中将本人可以用到的函数进行重写完善,笔者就此稍举例子:

 
  1.  
  2. namespace Notification;

  3.  
  4. //引入核心文件

  5. require_once('AndroidBroadcast.php');

  6. require_once('AndroidFilecast.php');

  7. require_once('AndroidGroupcast.php');

  8. require_once('AndroidUnicast.php');

  9. require_once('AndroidCustomizedcast.php');

  10. require_once('IOSBroadcast.php');

  11. require_once('IOSFilecast.php');

  12. require_once('IOSGroupcast.php');

  13. require_once('IOSUnicast.php');

  14. require_once('IOSCustomizedcast.php');

  15.  
  16.  
  17. class Sms {

  18. protected $appkey = NULL;

  19. protected $appMasterSecret = NULL;

  20. protected $timestamp = NULL;

  21. protected $validation_token = NULL;

  22.  
  23. function __construct($key, $secret) {

  24. $this->appkey = $key;

  25. $this->appMasterSecret = $secret;

  26. $this->timestamp = strval(time());

  27. }

  28.  
  29. /**

  30. * Android推送—广播

  31. * @param $title string 推送消息标题

  32. * @param $content string 推送消息内容

  33. * @return mixed

  34. */

  35. function sendAndroidBroadcast($title,$content) {

  36. try {

  37. $brocast = new AndroidBroadcast();

  38. $brocast->setAppMasterSecret($this->appMasterSecret);

  39. $brocast->setPredefinedKeyValue("appkey", $this->appkey);

  40. $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);

  41. $brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");

  42. $brocast->setPredefinedKeyValue("title", $title);

  43. $brocast->setPredefinedKeyValue("text", $content);

  44. $brocast->setPredefinedKeyValue("after_open", "go_app");

  45. $brocast->setPredefinedKeyValue("production_mode", "true");

  46. $brocast->setExtraField("test", "helloworld");

  47. print("Sending broadcast notification, please wait...\r\n");

  48. return $brocast->send();

  49. print("Sent SUCCESS\r\n");

  50. } catch (Exception $e) {

  51. print("Caught exception: " . $e->getMessage());

  52. }

  53. }

  54.  
  55. /**

  56. * Android推送—单播

  57. * @param $title string 推送消息标题

  58. * @param $content string 推送消息内容

  59. * @param $tokens array 设备的token值

  60. * @return mixed

  61. */

  62. function sendAndroidUnicast($title,$content,$tokens) {

  63. try {

  64. $unicast = new AndroidUnicast();

  65. $unicast->setAppMasterSecret($this->appMasterSecret);

  66. $unicast->setPredefinedKeyValue("appkey", $this->appkey);

  67. $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);

  68. $unicast->setPredefinedKeyValue("device_tokens", $tokens);

  69. $unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");

  70. $unicast->setPredefinedKeyValue("title", $title);

  71. $unicast->setPredefinedKeyValue("text", $content);

  72. $unicast->setPredefinedKeyValue("after_open", "go_app");

  73. $unicast->setPredefinedKeyValue("production_mode", "true");

  74. $unicast->setExtraField("test", "helloworld");

  75. print("Sending unicast notification, please wait...\r\n");

  76. return $unicast->send();

  77. print("Sent SUCCESS\r\n");

  78. } catch (Exception $e) {

  79. print("Caught exception: " . $e->getMessage());

  80. }

  81. }

  82. /**

  83. * IOS推送—广播

  84. * @param $title string 推送消息标题

  85. * @param $content string 推送消息内容

  86. * @return mixed

  87. */

  88. function sendIOSBroadcast($title,$content) {

  89. try {

  90. $brocast = new IOSBroadcast();

  91. $brocast->setAppMasterSecret($this->appMasterSecret);

  92. $brocast->setPredefinedKeyValue("appkey", $this->appkey);

  93. $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);

  94. $brocast->setPredefinedKeyValue("alert", $title);

  95. $brocast->setPredefinedKeyValue("badge", 0);

  96. $brocast->setPredefinedKeyValue("sound", "chime");

  97. $brocast->setPredefinedKeyValue("production_mode", "false");

  98. $brocast->setCustomizedField("test", $content);

  99. print("Sending broadcast notification, please wait...\r\n");

  100. return $brocast->send();

  101. print("Sent SUCCESS\r\n");

  102. } catch (Exception $e) {

  103. print("Caught exception: " . $e->getMessage());

  104. }

  105. }

  106.  
  107. /**

  108. * IOS推送—单播

  109. * @param $title string 推送消息标题

  110. * @param $content string 推送消息内容

  111. * @param $tokens array 设备的token值

  112. * @return mixed

  113. */

  114. function sendIOSUnicast($title,$content,$tokens) {

  115. try {

  116. $unicast = new IOSUnicast();

  117. $unicast->setAppMasterSecret($this->appMasterSecret);

  118. $unicast->setPredefinedKeyValue("appkey", $this->appkey);

  119. $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);

  120. $unicast->setPredefinedKeyValue("device_tokens", $tokens);

  121. $unicast->setPredefinedKeyValue("alert", $title);

  122. $unicast->setPredefinedKeyValue("badge", 0);

  123. $unicast->setPredefinedKeyValue("sound", "chime");

  124. $unicast->setPredefinedKeyValue("production_mode", "false");

  125. $unicast->setCustomizedField("test", $content);

  126. print("Sending unicast notification, please wait...\r\n");

  127. return $unicast->send();

  128. print("Sent SUCCESS\r\n");

  129. } catch (Exception $e) {

  130. print("Caught exception: " . $e->getMessage());

  131. }

  132. }

  133. }

 

5、在自己的系统中进行调用:

 
  1. $sms = new \Sms("5790414467e58ebc2f0008ae", "dgbtvr7myr3flllbpc6bww4gkfwjpnmv");

  2. $sms->sendAndroidBroadcast("这是标题","这是内容");

  3. $sms->sendAndroidUnicast("这是标题","这是内容",$tokens);

  4. $sms->sendIOSBroadcast("这是标题","这是内容");

  5. $sms->sendIOSUnicast("这是标题","这是内容",$tokens);

转载地址:https://blog.csdn.net/Zhihua_W/article/details/52250021?locationNum=12

你可能感兴趣的:(PHP,学习之路)