PushMsgCommon.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. <?php
  2. namespace App\Libs\Push\HuaWei;
  3. use App\Libs\Push\HuaWei\Admin\AndroidConfigDeliveryPriority;
  4. use App\Libs\Push\HuaWei\Admin\ApnConstant;
  5. use App\Libs\Push\HuaWei\Admin\Application;
  6. use App\Libs\Push\HuaWei\Admin\Constants;
  7. use App\Libs\Push\HuaWei\Admin\Msg\Android\AndroidConfig;
  8. use App\Libs\Push\HuaWei\Admin\Msg\Android\AndroidNotification;
  9. use App\Libs\Push\HuaWei\Admin\Msg\Android\Badge;
  10. use App\Libs\Push\HuaWei\Admin\Msg\Android\ClickAction;
  11. use App\Libs\Push\HuaWei\Admin\Msg\Android\LightSetting;
  12. use App\Libs\Push\HuaWei\Admin\Msg\Android\LightSettingColor;
  13. use App\Libs\Push\HuaWei\Admin\Msg\Apns\Alert;
  14. use App\Libs\Push\HuaWei\Admin\Msg\Apns\ApnsConfig;
  15. use App\Libs\Push\HuaWei\Admin\Msg\Apns\ApnsHeaders;
  16. use App\Libs\Push\HuaWei\Admin\Msg\Apns\ApnsHmsOptions;
  17. use App\Libs\Push\HuaWei\Admin\Msg\Apns\Aps;
  18. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppConfig;
  19. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppPushBody;
  20. use App\Libs\Push\HuaWei\Admin\Msg\InstanceApp\InstanceAppRingTone;
  21. use App\Libs\Push\HuaWei\Admin\Msg\Notification\Notification;
  22. use App\Libs\Push\HuaWei\Admin\Msg\PushMessage;
  23. use App\Libs\Push\HuaWei\Admin\Msg\WebPush\WebPushConfig;
  24. use App\Libs\Push\HuaWei\Admin\Msg\WebPush\WebPushHeaders;
  25. use App\Libs\Push\HuaWei\Admin\Msg\WebPush\WebPushHmsOptions;
  26. use App\Libs\Push\HuaWei\Admin\Msg\WebPush\WebPushNotification;
  27. use App\Libs\Push\HuaWei\Admin\Msg\WebPush\WebPushNotificationAction;
  28. use App\Libs\Push\HuaWei\Admin\NotificationPriority;
  29. use App\Libs\Push\HuaWei\Admin\PushConfig;
  30. use App\Libs\Push\HuaWei\Admin\PushLogConfig;
  31. class PushMsgCommon
  32. {
  33. // ordinal app
  34. private $appid;
  35. private $appsecret;
  36. // FOR PUSH MSG NOTIFICATION,PASSTHROUGH TOPIC/TOKEN/CONDITION
  37. public $hw_push_token_key;
  38. // FOR APN
  39. public $apn_push_token_key;
  40. // FOR WEBPUSH
  41. public $webpush_push_token_key;
  42. // fast app
  43. private $fast_appid;
  44. private $fast_appsecret;
  45. // fast app token
  46. public $fast_push_token;
  47. private $hw_token_server;
  48. private $hw_push_server;
  49. private $hw_push_token_query_server;
  50. private $log_suffix_show_start = ".............................";
  51. private $log_suffix_show_end = "-----------------------------";
  52. private $push_msg_type;
  53. private $default_topic = 'defaultTopic';
  54. private $str_len = 35;
  55. public function __construct()
  56. {
  57. $pushConfig = PushConfig::getSingleInstance();
  58. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_APPID", $this->str_len, '=') . ">" . $pushConfig->HW_APPID);
  59. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_APPSECRET", $this->str_len, '=') . ">" . $pushConfig->HW_APPSECRET);
  60. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_TOKEN_SERVER", $this->str_len, '=') . ">" . $pushConfig->HW_TOKEN_SERVER);
  61. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_PUSH_TOKEN_ARR", $this->str_len, '=') . ">" . $pushConfig->HW_PUSH_TOKEN_ARR);
  62. PushLogConfig::getSingleInstance()->LogMessage(str_pad("WEBPUSH_PUSH_TOKEN_ARR", $this->str_len, '=') . ">" . $pushConfig->WEBPUSH_PUSH_TOKEN_ARR);
  63. PushLogConfig::getSingleInstance()->LogMessage(str_pad("APN_PUSH_TOKEN_ARR", $this->str_len, '=') . ">" . $pushConfig->APN_PUSH_TOKEN_ARR);
  64. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_TOPIC_SUBSCRIBE_SERVER", $this->str_len, '=') . ">" . $pushConfig->HW_TOPIC_SUBSCRIBE_SERVER);
  65. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_TOPIC_UNSUBSCRIBE_SERVER", $this->str_len, '=') . ">" . $pushConfig->HW_TOPIC_UNSUBSCRIBE_SERVER);
  66. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_TOPIC_QUERY_SUBSCRIBER_SERVER", $this->str_len, '=') . ">" . $pushConfig->HW_TOPIC_QUERY_SUBSCRIBER_SERVER);
  67. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_FAST_APPID", $this->str_len, '=') . ">" . $pushConfig->HW_FAST_APPID);
  68. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_FAST_APPSECRET", $this->str_len, '=') . ">" . $pushConfig->HW_FAST_APPSECRET);
  69. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_FAST_PUSH_TOKEN", $this->str_len, '=') . ">" . $pushConfig->HW_FAST_PUSH_TOKEN);
  70. PushLogConfig::getSingleInstance()->LogMessage(str_pad("HW_PUSH_TOKEN_QUERY_SERVER", $this->str_len, '=') . ">" . $pushConfig->HW_PUSH_TOKEN_QUERY_SERVER);
  71. $this->appsecret = $pushConfig->HW_APPSECRET;
  72. $this->appid = $pushConfig->HW_APPID;
  73. $this->hw_token_server = $pushConfig->HW_TOKEN_SERVER;
  74. $this->hw_push_server = $pushConfig->HW_PUSH_SERVER;
  75. $this->hw_push_token_key = $pushConfig->HW_PUSH_TOKEN_ARR;
  76. $this->apn_push_token_key = $pushConfig->APN_PUSH_TOKEN_ARR;
  77. $this->webpush_push_token_key = $pushConfig->WEBPUSH_PUSH_TOKEN_ARR;
  78. $this->fast_appsecret = $pushConfig->HW_FAST_APPSECRET;
  79. $this->fast_appid = $pushConfig->HW_FAST_APPID;
  80. $this->fast_push_token = $pushConfig->HW_FAST_PUSH_TOKEN;
  81. }
  82. function sendPushMsgMessageByMsgType($msg_type, $topic = "")
  83. {
  84. $application_server = $this->hw_push_server;
  85. $this->printLogMethodOperate("push msg start" . $this->log_suffix_show_start, __FUNCTION__ . ':' . __LINE__);
  86. $this->push_msg_type = $msg_type;
  87. $message = $this->getMessageByMsgType($msg_type);
  88. $this->printLogMethodOperate("msg body:" . json_encode($message->getFields()), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  89. $application = $this->createApplication($application_server);
  90. $this->printLogMethodOperate("application server:" . json_encode($application->getApplicationFields()), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  91. $application->push_send_msg($message->getFields());
  92. $this->printLogMethodOperate("push msg end" . $this->log_suffix_show_end, __FUNCTION__ . ':' . __LINE__);
  93. }
  94. function sendPushMsgRealMessage($message, $push_msg_type = "")
  95. {
  96. $this->printLogMethodOperate("sendPushMsgRealMessage start push_msg_type:" . $push_msg_type, __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  97. if (!empty($push_msg_type)) {
  98. $this->push_msg_type = $push_msg_type;
  99. }
  100. $this->printLogMethodOperate("sendPushMsgRealMessage start push_msg_type:" . $this->push_msg_type, __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  101. $application_server = $this->hw_push_server;
  102. $application = $this->createApplication($application_server);
  103. $this->printLogMethodOperate("application server:" . json_encode($application->getApplicationFields()), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  104. $application->push_send_msg($message);
  105. $this->printLogMethodOperate("push msg end" . $this->log_suffix_show_end, __FUNCTION__ . ':' . __LINE__);
  106. }
  107. function sendPushTopicMsgMessage($topic = "")
  108. {
  109. if (empty($topic)) {
  110. $topic = $this->default_topic;
  111. }
  112. $testTopicCommonSample = new TestTopicCommonSample($topic);
  113. $this->printLogMethodOperate("start subscribe topic:" . $topic, __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_INFO_LEVEL);
  114. // subscribe msg
  115. $testTopicCommonSample->sendTopicMessage(Constants::TOPIC_SUBSCRIBE_MSG_TYPE);
  116. // query subscribe msg
  117. $testTopicCommonSample->sendTopicMessage(Constants::TOPIC_SUBSCRIBE_QUERY_MSG_TYPE);
  118. }
  119. /**
  120. * $result==>{"msg":"success","code":"80000000","requestId":"157561883923402813000201",
  121. * "topics":[{"name":"defaultTopic","addDate":"2019-12-06"},
  122. * {"name":"push-test","addDate":"2019-12-06"},
  123. * {"name":"targetTopic","addDate":"2019-12-06"},
  124. * {"name":"weather","addDate":"2019-12-06"}]}
  125. */
  126. private function isTopicInTopicList($result, $topic)
  127. {
  128. $this->printLogMethodOperate("isTopicInTopicList topic[" . $topic . "],result:" . json_encode($result) . "", __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_INFO_LEVEL);
  129. if (empty($result)) {
  130. return FALSE;
  131. }
  132. $arrResult = json_decode(json_encode($result), true);
  133. $this->printLogMethodOperate("isTopicInTopicList arrResult:" . json_encode($arrResult) . "][code:" . $arrResult["code"], __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  134. if (empty($arrResult["code"])) {
  135. return FALSE;
  136. }
  137. $this->printLogMethodOperate("isTopicInTopicList code:" . $arrResult["code"], __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  138. if (!in_array($arrResult["code"], array(
  139. "80000000",
  140. 80000000
  141. ))) {
  142. return FALSE;
  143. }
  144. if (empty($arrResult["topics"])) {
  145. return FALSE;
  146. }
  147. $topicArr = $arrResult["topics"];
  148. $this->printLogMethodOperate("isTopicInTopicList topicArr:" . json_encode($topicArr), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  149. if (empty($topicArr)) {
  150. return FALSE;
  151. }
  152. foreach ($topicArr as $topicObject) {
  153. if ($topicObject["name"] == $topic) {
  154. return TRUE;
  155. }
  156. }
  157. $this->printLogMethodOperate("isTopicInTopicList False,topic is not subscribe", __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  158. return FALSE;
  159. }
  160. private function getDefaultAndroidNotificationContent($titel)
  161. {
  162. $prefixTitleData = '';
  163. switch ($this->push_msg_type) {
  164. case Constants::PUSHMSG_NOTIFICATION_MSG_TYPE:
  165. {
  166. $prefixTitleData = ' notification ';
  167. break;
  168. }
  169. case Constants::PUSHMSG_PASS_THROUGHT_MSG_TYPE:
  170. {
  171. $prefixTitleData = ' passthrough ';
  172. break;
  173. }
  174. case Constants::PUSHMSG_FASTAPP_MSG_TYPE:
  175. {
  176. $prefixTitleData = ' fastapp ';
  177. break;
  178. }
  179. case Constants::PUSHMSG_TOPIC_MSG_TYPE:
  180. {
  181. $prefixTitleData = ' topic ';
  182. break;
  183. }
  184. case Constants::PUSHMSG_CONDITION_MSG_TYPE:
  185. {
  186. $prefixTitleData = ' condition ';
  187. break;
  188. }
  189. case Constants::APN_MSG_TYPE:
  190. {
  191. $prefixTitleData = ' apn ';
  192. break;
  193. }
  194. case Constants::WEB_PUSH_MSG_TYPE:
  195. {
  196. $prefixTitleData = ' webpush ';
  197. break;
  198. }
  199. }
  200. return $prefixTitleData . $titel . $prefixTitleData;
  201. }
  202. private function createAndroidNotification()
  203. {
  204. // generate click_action msg body for android notification-3-click_action
  205. $click_action = new ClickAction();
  206. $click_action->type(2);
  207. $click_action->type(1);
  208. $click_action->intent("#Intent;compo=com.rvr/.Activity;S.W=U;end");
  209. $click_action->action("test add");
  210. $click_action->url("https://www.baidu.com");
  211. $click_action->rich_resource("test rich resource");
  212. $click_action->buildFields();
  213. // generate Badge for android notification-3-badge
  214. $badge = new Badge();
  215. $badge->add_num(99);
  216. $badge->setclass("Classic");
  217. $badge->set_num(99);
  218. $badge->buildFields();
  219. // generate Light Settings for android notification-3-badge
  220. $lightSetting = new LightSetting();
  221. $lightSetting->light_on_duration("3.5");
  222. $lightSetting->light_off_duration("5S");
  223. // set light setting color
  224. $LightSettingColor = new LightSettingColor();
  225. $LightSettingColor->setgenFullcolor(0, 0, 1, 1);
  226. $LightSettingColor->buildFields();
  227. $lightSetting->color($LightSettingColor->getFields());
  228. $lightSetting->buildFields();
  229. // 构建android notification消息体-2 for android config
  230. $android_notification = new AndroidNotification();
  231. $android_notification->title($this->getDefaultAndroidNotificationContent("default hw title "));
  232. $android_notification->body($this->getDefaultAndroidNotificationContent("default hw body"));
  233. $android_notification->icon("https://res.vmallres.com/pimages//common/config/logo/SXppnESYv4K11DBxDFc2.png");
  234. $android_notification->color("#AACCDD");
  235. $android_notification->sound("https://att.chinauui.com/day_120606/20120606_7fcf2235b44f1eab0b4dadtAkAGMTBHK.mp3");
  236. $android_notification->tag("tagBoom");
  237. $android_notification->body_loc_key("M.String.body");
  238. $android_notification->body_loc_args(array(
  239. "Boy",
  240. "Dog"
  241. ));
  242. $android_notification->title_loc_key("M.String.title");
  243. $android_notification->title_loc_args(array(
  244. "Girl",
  245. "Cat"
  246. ));
  247. $android_notification->channel_id("RingRing");
  248. $android_notification->notify_summary("Some Summary");
  249. $android_notification->image("https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png");
  250. $android_notification->style(1);
  251. $android_notification->big_title("Big Boom Title");
  252. $android_notification->big_body("Big Boom Body");
  253. $android_notification->auto_clear(86400000);
  254. $android_notification->notify_id(486);
  255. $android_notification->group("Espace");
  256. $android_notification->importance(NotificationPriority::NOTIFICATION_PRIORITY_NORMAL);
  257. $android_notification->ticker("i am a ticker");
  258. $android_notification->auto_cancel(false);
  259. $android_notification->when("2019-11-05");
  260. $android_notification->use_default_vibrate(true);
  261. $android_notification->use_default_light(false);
  262. $android_notification->visibility("PUBLIC");
  263. $android_notification->foreground_show(true);
  264. $android_notification->vibrate_config(array(
  265. "1.5",
  266. "2.000000001",
  267. "3"
  268. ));
  269. $android_notification->click_action($click_action->getFields());
  270. $android_notification->badge($badge->getFields());
  271. $android_notification->light_settings($lightSetting->getFields());
  272. $android_notification->buildFields();
  273. return $android_notification;
  274. }
  275. private function createAndroidConfig()
  276. {
  277. $android_notification = $this->createAndroidNotification();
  278. $android_config = new AndroidConfig();
  279. $android_config->collapse_key(-1);
  280. $android_config->urgency(AndroidConfigDeliveryPriority::PRIORITY_HIGH);
  281. $android_config->ttl("1448s");
  282. $android_config->bi_tag("Trump");
  283. if ($this->push_msg_type == Constants::PUSHMSG_FASTAPP_MSG_TYPE) {
  284. $android_config->fast_app_target(1);
  285. } else {
  286. $android_config->notification($android_notification->getFields());
  287. }
  288. $android_config->buildFields();
  289. return $android_config;
  290. }
  291. private function createNotification()
  292. {
  293. $notification = new Notification("Big News", "This is a Big News!", "https://res.vmallres.com/pimages//common/config/logo/SXppnESYv4K11DBxDFc2_0.png");
  294. $notification->buildFields();
  295. return $notification;
  296. }
  297. private function createApnsConfig()
  298. {
  299. // ApnsHeaders
  300. $apnsHeaders = new ApnsHeaders();
  301. $apnsHeaders->apns_topic("hmspush");
  302. $apnsHeaders->apns_priority(ApnConstant::ANP_PRIORITY_SEND_IMMEDIATELY);
  303. $apnsHeaders->buildFields();
  304. // ApnHmsOptions
  305. $apnsHmsOptions = new ApnsHmsOptions();
  306. $apnsHmsOptions->target_user_type(ApnConstant::APN_TARGET_USER_TEST_USER);
  307. $apnsHmsOptions->buildFields();
  308. // Aps
  309. // Alert
  310. $alert = new Alert();
  311. $alert->title("hw default ios message title");
  312. $alert->body("hw default ios message body");
  313. $alert->action_loc_key("PLAY");
  314. $alert->buildFields();
  315. $aps = new Aps();
  316. $aps->alert($alert->getFields());
  317. $aps->badge(5);
  318. $aps->buildFields();
  319. $apnsConfig = new ApnsConfig();
  320. $apnsConfig->headers($apnsHeaders->getFields());
  321. $apnsConfig->hms_options($apnsHmsOptions->getFields());
  322. $apn_payload["aps"] = $aps->getFields();
  323. $apn_payload["acme1"] = "bar";
  324. $apn_payload["acme2"] = array(
  325. "bang",
  326. "whiz"
  327. );
  328. $apnsConfig->payload($apn_payload);
  329. $apnsConfig->buildFields();
  330. return $apnsConfig;
  331. }
  332. private function createWebPush()
  333. {
  334. $webPushConfig = new WebPushConfig();
  335. $webPushConfig->data("test webpush data");
  336. $webPushHeaders = new WebPushHeaders();
  337. $webPushHeaders->topic("12313ceshi");
  338. $webPushHeaders->ttl("990");
  339. $webPushHeaders->urgency(Constants::WEBPUSH_URGENCY_VERY_LOW);
  340. $webPushHeaders->buildFields();
  341. $webPushConfig->headers($webPushHeaders->getFields());
  342. $webPushHmsOptions = new WebPushHmsOptions();
  343. $webPushHmsOptions->link("https://www.huawei.com/");
  344. $webPushHmsOptions->buildFields();
  345. $webPushConfig->hmsOptions($webPushHmsOptions->getFields());
  346. $webPUshNotionfication = new WebPushNotification();
  347. $webPUshNotionfication->title("notication string");
  348. $webPUshNotionfication->body("web push body");
  349. $webPUshNotionfication->icon("https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png");
  350. $webPUshNotionfication->image("https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png");
  351. $webPUshNotionfication->lang("string");
  352. $webPUshNotionfication->tag("string");
  353. $webPUshNotionfication->badge("string");
  354. $webPUshNotionfication->dir("auto");
  355. $webPUshNotionfication->vibrate(array(
  356. 1,
  357. 2,
  358. 3
  359. ));
  360. $webPUshNotionfication->renotify(false);
  361. $webPUshNotionfication->require_interaction(false);
  362. $webPUshNotionfication->silent(false);
  363. $webPUshNotionfication->timestamp(1545201266);
  364. $webPushNotificationAction = new WebPushNotificationAction();
  365. $webPushNotificationAction->title("string");
  366. $webPushNotificationAction->action("123");
  367. $webPushNotificationAction->icon("https://developer-portalres-drcn.dbankcdn.com/system/modules/org.opencms.portal.template.core/resources/images/icon_Promotion.png");
  368. $webPushNotificationAction->buildFields();
  369. $webPUshNotionfication->actions(array(
  370. $webPushNotificationAction->getFields()
  371. ));
  372. $webPUshNotionfication->buildFields();
  373. $webPushConfig->notification($webPUshNotionfication->getFields());
  374. $webPushConfig->buildFields();
  375. return $webPushConfig;
  376. }
  377. private function printLogMethodOperate($dataFlow, $functionName = "", $logLevel = "")
  378. {
  379. $logModule = Constants::HW_PUSH_LOG_PUSH_MSG_MODULE;
  380. if (empty($logLevel)) {
  381. $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL;
  382. }
  383. if (empty($functionName)) {
  384. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . $dataFlow, $logLevel, $logModule);
  385. } else {
  386. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . '[' . $functionName . ']' . $dataFlow, $logLevel, $logModule);
  387. }
  388. }
  389. private function createApplication($application_server)
  390. {
  391. $this->printLogMethodOperate("createApplication push_msg_type:" . $this->push_msg_type, __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  392. if ($this->push_msg_type == Constants::PUSHMSG_FASTAPP_MSG_TYPE) {
  393. $this->printLogMethodOperate("createApplication PUSHMSG_FASTAPP_MSG_TYPE", __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  394. $application = new Application($this->fast_appid, $this->fast_appsecret, $this->hw_token_server, $application_server, $this->hw_push_token_query_server);
  395. return $application;
  396. }
  397. $application = new Application($this->appid, $this->appsecret, $this->hw_token_server, $application_server, $this->hw_push_token_query_server);
  398. return $application;
  399. }
  400. private function getMessageByMsgType($msg_type)
  401. {
  402. switch ($msg_type) {
  403. case Constants::PUSHMSG_NOTIFICATION_MSG_TYPE:
  404. {
  405. return $this->createNotificationMsg();
  406. }
  407. case Constants::PUSHMSG_PASS_THROUGHT_MSG_TYPE:
  408. {
  409. return $this->createPassThroughMsg();
  410. }
  411. case Constants::PUSHMSG_FASTAPP_MSG_TYPE:
  412. {
  413. return $this->createFastAppMsg();
  414. }
  415. case Constants::PUSHMSG_TOPIC_MSG_TYPE:
  416. {
  417. return $this->createTopicMsg();
  418. }
  419. case Constants::PUSHMSG_CONDITION_MSG_TYPE:
  420. {
  421. return $this->createConditionMsg();
  422. }
  423. case Constants::APN_MSG_TYPE:
  424. {
  425. return $this->createApnsMsg();
  426. }
  427. case Constants::WEB_PUSH_MSG_TYPE:
  428. {
  429. return $this->createWebPushMsg();
  430. }
  431. }
  432. }
  433. private function createFastAppConfigNotificationData()
  434. {
  435. $instanceAppConfig = new InstanceAppConfig();
  436. $instanceAppConfig->pushtype(0);
  437. $instanceAppPushbody = new InstanceAppPushBody();
  438. $instanceAppPushbody->title("test fast app");
  439. $instanceAppPushbody->description("test fast app description");
  440. $instanceAppPushbody->page("/");
  441. $instanceAppPushbody->params(array(
  442. "key1" => "test1",
  443. "key2" => "test2"
  444. ));
  445. $instanceAppRingtone = new InstanceAppRingTone();
  446. $instanceAppRingtone->breathLight(true);
  447. $instanceAppRingtone->vibration(true);
  448. $instanceAppRingtone->buildFields();
  449. $instanceAppPushbody->ringtone($instanceAppRingtone->getFields());
  450. $instanceAppPushbody->buildFields();
  451. $instanceAppConfig->pushbody($instanceAppPushbody->getFields());
  452. $instanceAppConfig->buildFields();
  453. return $instanceAppConfig;
  454. }
  455. private function createFastAppConfigPassThroughData()
  456. {
  457. $instanceAppConfig = new InstanceAppConfig();
  458. $instanceAppConfig->pushtype(1);
  459. $instanceAppPushbody = new InstanceAppPushbody();
  460. $instanceAppPushbody->messageId("111110001");
  461. $instanceAppPushbody->data("hw default passthroug test");
  462. $instanceAppPushbody->buildFields();
  463. $instanceAppConfig->pushbody($instanceAppPushbody->getFields());
  464. $instanceAppConfig->buildFields();
  465. return $instanceAppConfig;
  466. }
  467. private function createFastAppMsg()
  468. {
  469. $this->printLogMethodOperate("push msg notification start", __FUNCTION__ . ':' . __LINE__);
  470. $message = new PushMessage();
  471. $message->data($this->createFastAppConfigNotificationData()->getFields());
  472. $message->android($this->createAndroidConfig()
  473. ->getFields());
  474. $message->token(array(
  475. $this->fast_push_token
  476. ));
  477. $message->buildFields();
  478. $this->printLogMethodOperate("push msg notification end", __FUNCTION__ . ':' . __LINE__);
  479. return $message;
  480. }
  481. private function createNotificationMsg()
  482. {
  483. $this->printLogMethodOperate("push msg notification start", __FUNCTION__ . ':' . __LINE__);
  484. $message = new PushMessage();
  485. $message->android($this->createAndroidConfig()
  486. ->getFields());
  487. $message->notification($this->createNotification()
  488. ->getFields());
  489. $message->token(array(
  490. $this->hw_push_token_key
  491. ));
  492. $message->buildFields();
  493. $this->printLogMethodOperate("push msg notification end", __FUNCTION__ . ':' . __LINE__);
  494. return $message;
  495. }
  496. private function createTopicMsg()
  497. {
  498. $this->printLogMethodOperate("push msg createTopicMsg start", __FUNCTION__ . ':' . __LINE__);
  499. $message = new PushMessage();
  500. $message->android($this->createAndroidConfig()
  501. ->getFields());
  502. // $message->notification($this->createNotification()->buildFields());
  503. $message->topic($this->default_topic);
  504. $message->buildFields();
  505. $this->printLogMethodOperate("push msg createTopicMsg end", __FUNCTION__ . ':' . __LINE__);
  506. return $message;
  507. }
  508. private function createConditionMsg()
  509. {
  510. $this->printLogMethodOperate("push msg createTopicMsg start", __FUNCTION__ . ':' . __LINE__);
  511. $message = new PushMessage();
  512. $message->android($this->createAndroidConfig()
  513. ->getFields());
  514. // $message->notification($this->createNotification()->buildFields());
  515. $message->condition("'defaultTopic' in topics");
  516. // $message->condition("'weather' in topics || ('TopicB' in topics && 'TopicC' in topics)");
  517. $message->buildFields();
  518. $this->printLogMethodOperate("push msg createTopicMsg end", __FUNCTION__ . ':' . __LINE__);
  519. return $message;
  520. }
  521. private function createPassThroughMsg()
  522. {
  523. $this->printLogMethodOperate("push msg passthrough start", __FUNCTION__ . ':' . __LINE__);
  524. $message = new PushMessage();
  525. $message->data("1111");
  526. $message->token(array(
  527. $this->hw_push_token_key
  528. ));
  529. $message->buildFields();
  530. $this->printLogMethodOperate("push msg passthrough end", __FUNCTION__ . ':' . __LINE__);
  531. return $message;
  532. }
  533. private function createApnsMsg()
  534. {
  535. $this->printLogMethodOperate("push msg apns start", __FUNCTION__ . ':' . __LINE__);
  536. $message = new PushMessage();
  537. $apnsConfig = $this->createApnsConfig();
  538. $message->apns($apnsConfig->getFields());
  539. $message->token(array(
  540. $this->apn_push_token_key
  541. ));
  542. $message->buildFields();
  543. $this->printLogMethodOperate("push msg apns end", __FUNCTION__ . ':' . __LINE__);
  544. return $message;
  545. }
  546. private function createWebPushMsg()
  547. {
  548. $this->printLogMethodOperate("push msg webpush start", __FUNCTION__ . ':' . __LINE__);
  549. $message = new PushMessage();
  550. $message->webpush($this->createWebPush()
  551. ->getFields());
  552. $message->token(array(
  553. $this->webpush_push_token_key
  554. ));
  555. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . '[web-token:' . json_encode($message->get_token()) . ']', Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  556. $message->buildFields();
  557. $this->printLogMethodOperate("push msg webpush end", __FUNCTION__ . ':' . __LINE__);
  558. return $message;
  559. }
  560. }
  561. class TestTopicCommonSample
  562. {
  563. private $topic = "defaultTopic";
  564. private $topic_msg_create_type = 1;
  565. private $appsecret;
  566. private $appid;
  567. private $hw_token_server;
  568. private $hw_topic_subscriber_server;
  569. private $hw_topic_unsubscriber_server;
  570. private $hw_topic_query_subscriber_server;
  571. private $tokenServerKey;
  572. private $log_suffix_show = ".............................";
  573. public function __construct($topic_value = "", $default_msg_create_type = "")
  574. {
  575. if (!empty($topic_value)) {
  576. $this->topic = $topic_value;
  577. }
  578. if (!empty($default_msg_create_type)) {
  579. $this->topic_msg_create_type = $default_msg_create_type;
  580. }
  581. $pushConfig = PushConfig::getSingleInstance();
  582. $this->appsecret = $pushConfig->HW_APPSECRET;
  583. $this->appid = $pushConfig->HW_APPID;
  584. $this->hw_token_server = $pushConfig->HW_TOKEN_SERVER;
  585. $this->tokenServerKey = $pushConfig->HW_PUSH_TOKEN_ARR;
  586. $this->hw_topic_subscriber_server = $pushConfig->HW_TOPIC_SUBSCRIBE_SERVER;
  587. $this->hw_topic_unsubscriber_server = $pushConfig->HW_TOPIC_UNSUBSCRIBE_SERVER;
  588. $this->hw_topic_query_subscriber_server = $pushConfig->HW_TOPIC_QUERY_SUBSCRIBER_SERVER;
  589. }
  590. private function createTopicData()
  591. {
  592. $topicMsg = new TopicMsg();
  593. $topicMsg->topic($this->topic);
  594. $topicMsg->tokenArray(array(
  595. $this->tokenServerKey
  596. ));
  597. $topicMsg->buildFields();
  598. return $topicMsg;
  599. }
  600. private function createApplication($application_server)
  601. {
  602. $application = new Application($this->appid, $this->appsecret, $this->hw_token_server, $application_server);
  603. return $application;
  604. }
  605. private function printLogMethodOperate($msg_type, $dataFlow, $functionName = "", $logLevel = "")
  606. {
  607. $dataFlow = 'subscribe topic ' . $dataFlow;
  608. $logModule = Constants::HW_PUSH_LOG_TOPIC_SUBSCRIBE_MODULE;
  609. switch ($msg_type) {
  610. case Constants::TOPIC_UNSUBSCRIBE_MSG_TYPE:
  611. {
  612. $dataFlow = 'unsubscribe topic' . $dataFlow;
  613. $logModule = Constants::HW_PUSH_LOG_TOPIC_UNSUBSCRIBE_MODULE;
  614. }
  615. break;
  616. }
  617. if (empty($logLevel)) {
  618. $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL;
  619. }
  620. if (empty($functionName)) {
  621. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . $dataFlow . $this->log_suffix_show, $logLevel, $logModule);
  622. } else {
  623. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . '[' . $functionName . ']' . $dataFlow . $this->log_suffix_show, $logLevel, $logModule);
  624. }
  625. }
  626. private function printLogMsgOperate($msg_type, $dataFlow, $functionName = "", $logLevel = "")
  627. {
  628. $logModule = Constants::HW_PUSH_LOG_TOPIC_SUBSCRIBE_MODULE;
  629. switch ($msg_type) {
  630. case Constants::TOPIC_UNSUBSCRIBE_MSG_TYPE:
  631. {
  632. $logModule = Constants::HW_PUSH_LOG_TOPIC_UNSUBSCRIBE_MODULE;
  633. }
  634. break;
  635. }
  636. if (empty($logLevel)) {
  637. $logLevel = Constants::HW_PUSH_LOG_INFO_LEVEL;
  638. }
  639. if (empty($functionName)) {
  640. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . $dataFlow . $this->log_suffix_show, $logLevel, $logModule);
  641. } else {
  642. PushLogConfig::getSingleInstance()->LogMessage('[' . __CLASS__ . ']' . '[' . $functionName . ']' . $dataFlow . $this->log_suffix_show, $logLevel, $logModule);
  643. }
  644. }
  645. /**
  646. * topic subscribe/unsubscribe
  647. */
  648. function sendTopicMessage($msg_type)
  649. {
  650. $this->printLogMethodOperate($msg_type, "start", __FUNCTION__ . ':' . __LINE__);
  651. $topicMsg = $this->createTopicData();
  652. if ($this->topic_msg_create_type == 1) {
  653. $this->printLogMsgOperate($msg_type, "topicMsg:" . json_encode($topicMsg->getFields()), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  654. }
  655. $application_server = $this->hw_topic_subscriber_server;
  656. if ($msg_type == Constants::TOPIC_UNSUBSCRIBE_MSG_TYPE) {
  657. $application_server = $this->hw_topic_unsubscriber_server;
  658. } else if ($msg_type == Constants::TOPIC_SUBSCRIBE_QUERY_MSG_TYPE) {
  659. $application_server = $this->hw_topic_query_subscriber_server;
  660. $topicMsg = array(
  661. 'token' => $this->tokenServerKey
  662. );
  663. }
  664. $application = $this->createApplication($application_server);
  665. $this->printLogMsgOperate($msg_type, "application server:" . json_encode($application->getApplicationFields()), __FUNCTION__ . ':' . __LINE__, Constants::HW_PUSH_LOG_DEBUG_LEVEL);
  666. $topicResult = "";
  667. if ($msg_type == Constants::TOPIC_SUBSCRIBE_QUERY_MSG_TYPE) {
  668. $topicResult = $application->common_send_msg($topicMsg);
  669. } else {
  670. $topicResult = $application->common_send_msg($topicMsg->getFields());
  671. }
  672. $this->printLogMethodOperate($msg_type, "end", __FUNCTION__ . ':' . __LINE__);
  673. return $topicResult;
  674. }
  675. }
  676. class TopicMsg
  677. {
  678. // madatory
  679. private $topic;
  680. // madatory
  681. private $tokenArray;
  682. private $fields;
  683. public function __construct()
  684. {
  685. $this->fields = array();
  686. }
  687. public function topic($value)
  688. {
  689. $this->topic = $value;
  690. }
  691. public function tokenArray($value)
  692. {
  693. $this->tokenArray = $value;
  694. }
  695. public function getFields()
  696. {
  697. return $this->fields;
  698. }
  699. public function buildFields()
  700. {
  701. $keys = array(
  702. 'topic',
  703. 'tokenArray'
  704. );
  705. foreach ($keys as $key) {
  706. if (isset($this->$key)) {
  707. $this->fields[$key] = $this->$key;
  708. }
  709. }
  710. }
  711. }