资讯专栏INFORMATION COLUMN

PHP支持友盟消息推送踩坑记录

scq000 / 1860人阅读

摘要:公司客户端选定的友盟消息推送文档很少自己踩坑不少代码引入核心文件推送广播推送消息标题推送消息内容推送单播推送消息标题推送消息内容设备的值自定义

公司客户端选定的友盟消息推送,PHP文档很少,自己踩坑不少:

代码

</>复制代码

  1. andkey = "andriodkey";
  2. $this->andMasterSecret = "andriodsecret";
  3. //ios
  4. $this->ioskey = "ioskey";
  5. $this->iosMasterSecret = "iossecret";
  6. $this->timestamp = strval(time());
  7. }
  8. /**
  9. * Android推送—广播
  10. * @param $title string 推送消息标题
  11. * @param $content string 推送消息内容
  12. * @return mixed
  13. */
  14. function sendAndroidBroadcast($title, $content)
  15. {
  16. try {
  17. $brocast = new AndroidBroadcast();
  18. $brocast->setAppMasterSecret($this->andMasterSecret);
  19. $brocast->setPredefinedKeyValue("appkey", $this->andkey);
  20. $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
  21. $brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");
  22. $brocast->setPredefinedKeyValue("title", $title);
  23. $brocast->setPredefinedKeyValue("text", $content);
  24. $brocast->setPredefinedKeyValue("after_open", "go_app");
  25. $brocast->setPredefinedKeyValue("production_mode", "true");
  26. $brocast->setExtraField("test", "helloworld");
  27. return $brocast->send();
  28. } catch (Exception $e) {
  29. print("Caught exception: " . $e->getMessage());
  30. }
  31. }
  32. /**
  33. * Android推送—单播
  34. * @param $title string 推送消息标题
  35. * @param $content string 推送消息内容
  36. * @param $tokens array 设备的token值
  37. * @return mixed
  38. */
  39. function sendAndroidUnicast($title, $content, $tokens)
  40. {
  41. try {
  42. $unicast = new AndroidUnicast();
  43. $unicast->setAppMasterSecret($this->andMasterSecret);
  44. $unicast->setPredefinedKeyValue("appkey", $this->andkey);
  45. $unicast->setPredefinedKeyValue("mipush", true);
  46. $unicast->setPredefinedKeyValue("mi_activity", "cn.weidijia.pccm.ui.activity.SplashActivity");
  47. $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
  48. $unicast->setPredefinedKeyValue("device_tokens", $tokens);
  49. $unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");
  50. $unicast->setPredefinedKeyValue("title", $title);
  51. $unicast->setPredefinedKeyValue("text", $content);
  52. $unicast->setPredefinedKeyValue("after_open", "go_app");
  53. $unicast->setPredefinedKeyValue("production_mode", "true");
  54. $unicast->setExtraField("test", "helloworld");
  55. return $unicast->send();
  56. } catch (Exception $e) {
  57. print("Caught exception: " . $e->getMessage());
  58. }
  59. }
  60. /*
  61. * android自定义
  62. * */
  63. function sendAndroidCustomizedcast($alias, $alias_type, $ticker, $title, $text)
  64. {
  65. try {
  66. $customizedcast = new AndroidCustomizedcast();
  67. $customizedcast->setAppMasterSecret($this->andMasterSecret);
  68. $customizedcast->setPredefinedKeyValue("appkey", $this->andkey);
  69. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  70. $customizedcast->setPredefinedKeyValue("alias", $alias);
  71. $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
  72. $customizedcast->setPredefinedKeyValue("ticker", $ticker);
  73. $customizedcast->setPredefinedKeyValue("title", $title);
  74. $customizedcast->setPredefinedKeyValue("text", $text);
  75. $customizedcast->setPredefinedKeyValue("after_open", "go_app");
  76. return $customizedcast->send();
  77. } catch (Exception $e) {
  78. print("Caught exception: " . $e->getMessage());
  79. }
  80. }
  81. /**
  82. * IOS推送—广播
  83. * @param $title string 推送消息标题
  84. * @param $content string 推送消息内容
  85. * @return mixed
  86. */
  87. function sendIOSBroadcast($title, $content)
  88. {
  89. try {
  90. $brocast = new IOSBroadcast();
  91. $brocast->setAppMasterSecret($this->iosMasterSecret);
  92. $brocast->setPredefinedKeyValue("appkey", $this->ioskey);
  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. return $brocast->send();
  100. } catch (Exception $e) {
  101. print("Caught exception: " . $e->getMessage());
  102. }
  103. }
  104. /**
  105. * IOS推送—单播
  106. * @param $title string 推送消息标题
  107. * @param $content string 推送消息内容
  108. * @param $tokens array 设备的token值
  109. * @return mixed
  110. */
  111. function sendIOSUnicast($title, $content, $tokens)
  112. {
  113. try {
  114. $unicast = new IOSUnicast();
  115. $unicast->setAppMasterSecret($this->iosMasterSecret);
  116. $unicast->setPredefinedKeyValue("appkey", $this->ioskey);
  117. $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
  118. $unicast->setPredefinedKeyValue("device_tokens", $tokens);
  119. $unicast->setPredefinedKeyValue("alert", $title);
  120. $unicast->setPredefinedKeyValue("badge", 0);
  121. $unicast->setPredefinedKeyValue("sound", "chime");
  122. $unicast->setPredefinedKeyValue("production_mode", "false");
  123. $unicast->setCustomizedField("test", $content);
  124. return $unicast->send();
  125. } catch (Exception $e) {
  126. print("Caught exception: " . $e->getMessage());
  127. }
  128. }
  129. /*
  130. * IOS自定义
  131. * */
  132. function sendIOSCustomizedcast($alias, $alias_type, $ticker, $title, $text)
  133. {
  134. $alert = [
  135. "title" => $title,
  136. "subtitle" => $ticker,
  137. "body" => $text
  138. ];
  139. try {
  140. $customizedcast = new IOSCustomizedcast();
  141. $customizedcast->setAppMasterSecret($this->iosMasterSecret);
  142. $customizedcast->setPredefinedKeyValue("appkey", $this->ioskey);
  143. $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
  144. $customizedcast->setPredefinedKeyValue("alias", $alias);
  145. $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
  146. $customizedcast->setPredefinedKeyValue("alert", $alert);
  147. $customizedcast->setPredefinedKeyValue("badge", 0);
  148. $customizedcast->setPredefinedKeyValue("sound", "chime");
  149. $customizedcast->setPredefinedKeyValue("production_mode", "false");
  150. return $customizedcast->send();
  151. } catch (Exception $e) {
  152. print("Caught exception: " . $e->getMessage());
  153. }
  154. }
  155. }

andriod和ios使用不同的key和secret,申请就好.下载的demo里面没有列播,网上也没有资料,客服就更别说了...

ios的自定义播送的alert要传递数组

离线推送这两个字段需要在UmengNotification.php文件内添加这两个字段,否则会报错.

</>复制代码

  1. protected $DATA_KEYS = array("appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id", "filter", "production_mode",
  2. "feedback", "description", "thirdparty_id", "mipush", "mi_activity");

友盟U-push参数解析

3.使用下载的官方demo一定要吧print都注释掉.

第一篇写的很乱,不好意思


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29069.html

相关文章

  • 运营必看!依托友盟大数据优势,各类 App “双十一”推送攻略

    摘要:友盟推送后台数据显示,当在第一时间推送重大新闻时的打开率最高,最高甚至可以超过。友盟微推送结合友盟大数据优势,统计出固定天数不活跃的用户群。 双十一刚过,相信不少开发者小伙伴还沉浸在剁手的余温中,刚刚帮开发者在双十一当天推送了 191,991,9272 条消息的友盟消息推送团队,想借着双十一的热乎劲儿,为开发者小伙伴们带来一些实用的推送技巧!一起来看一看以下三种类型的 App 在这个双...

    URLOS 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<