ThinkPHP框架整合友盟推送DEMO

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

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

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

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

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

  1. //命名空间

  2. namespace Notification;


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

 
  1. <?php

  2.  
  3. namespace Notification;

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

  6. require_once('AndroidBroadcast.php');

  7. require_once('AndroidFilecast.php');

  8. require_once('AndroidGroupcast.php');

  9. require_once('AndroidUnicast.php');

  10. require_once('AndroidCustomizedcast.php');

  11. require_once('IOSBroadcast.php');

  12. require_once('IOSFilecast.php');

  13. require_once('IOSGroupcast.php');

  14. require_once('IOSUnicast.php');

  15. require_once('IOSCustomizedcast.php');

  16.  
  17.  
  18. class Sms {

  19. protected $appkey = NULL;

  20. protected $appMasterSecret = NULL;

  21. protected $timestamp = NULL;

  22. protected $validation_token = NULL;

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

  25. $this->appkey = $key;

  26. $this->appMasterSecret = $secret;

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

  28. }

  29.  
  30. /**

  31. * Android推送—广播

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

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

  34. * @return mixed

  35. */

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

  37. try {

  38. $brocast = new AndroidBroadcast();

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

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

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

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

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

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

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

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

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

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

  49. return $brocast->send();

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

  51. } catch (Exception $e) {

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

  53. }

  54. }

  55.  
  56. /**

  57. * Android推送—单播

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

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

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

  61. * @return mixed

  62. */

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

  64. try {

  65. $unicast = new AndroidUnicast();

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

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

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

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

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

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

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

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

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

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

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

  77. return $unicast->send();

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

  79. } catch (Exception $e) {

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

  81. }

  82. }<pre name="code" class="php">

  83. <span style="white-space:pre"> </span>/**

  84. * IOS推送—广播

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

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

  87. * @return mixed

  88. */

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

  90. try {

  91. $brocast = new IOSBroadcast();

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

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

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

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

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

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

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

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

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

  101. return $brocast->send();

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

  103. } catch (Exception $e) {

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

  105. }

  106. }

  107.  
  108. /**

  109. * IOS推送—单播

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

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

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

  113. * @return mixed

  114. */

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

  116. try {

  117. $unicast = new IOSUnicast();

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

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

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

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

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

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

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

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

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

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

  128. return $unicast->send();

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

  130. } catch (Exception $e) {

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

  132. }

  133. }<pre name="code" class="php">

  134. }

 

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

猜你喜欢

转载自blog.csdn.net/qq_32364939/article/details/81740654