Test.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/12/3 0003
  6. * Time: 9:49
  7. */
  8. namespace app\api\controller;
  9. use app\common\controller\Api;
  10. use DateTime;
  11. use onesignal\client\api\DefaultApi;
  12. use onesignal\client\Configuration;
  13. use onesignal\client\model\GetNotificationRequestBody;
  14. use onesignal\client\model\Notification;
  15. use onesignal\client\model\StringMap;
  16. use onesignal\client\model\Player;
  17. use onesignal\client\model\UpdatePlayerTagsRequestBody;
  18. use onesignal\client\model\ExportPlayersRequestBody;
  19. use onesignal\client\model\Segment;
  20. use onesignal\client\model\FilterExpressions;
  21. use GuzzleHttp;
  22. use app\common\model\User;
  23. use think\Db;
  24. use think\Queue;
  25. use think\Request;
  26. class Test extends Api
  27. {
  28. protected $noNeedLogin = ['*'];
  29. protected $noNeedRight = ['*'];
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $url = $_SERVER['HTTP_HOST'];
  34. if (strpos($url, 'westkiss') !== false) {
  35. $appKeyToken='MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh';
  36. $userKeyToken='YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh';
  37. }elseif (strpos($url, 'asteriahair') !== false) {
  38. $appKeyToken='NGU4ZTU0OGUtMGU2Yy00NjNmLTgwNWItZTVmYjQxODg5Yzhh';
  39. $userKeyToken='NjQwYWRlYWEtODlmZS00ZDFhLThmZmMtN2E0ZTQwZWI2NmY5';
  40. }else{
  41. $appKeyToken='NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx';
  42. $userKeyToken='NWU3OGMxNzgtZjEzNS00M2JmLThmNzItZjY5YzIxOWU5YTQ3';
  43. }
  44. $config= Configuration::getDefaultConfiguration()
  45. ->setAppKeyToken($appKeyToken)
  46. ->setUserKeyToken($userKeyToken);
  47. $this->apiInstance = new DefaultApi(
  48. new GuzzleHttp\Client(),
  49. $config
  50. );
  51. }
  52. public function test()
  53. {
  54. $data =input();
  55. $id=$data['ids'];
  56. $wheres['id']=$id;
  57. $one_push = Db::name('message')->where($wheres)->find();
  58. if (empty($one_push)) {
  59. echo '没有查到推送队列';
  60. exit;
  61. }
  62. $result =$this->sendTest($one_push);
  63. $this->success('',$result);
  64. }
  65. function createNotification($one_push){
  66. $template_dsc =$one_push['template_dsc'];
  67. $title =$one_push['template_name'];
  68. $type =$one_push['type'];
  69. $url =$one_push['para'];
  70. $content = new StringMap();
  71. $content->setEn($template_dsc);
  72. $notification = new Notification();
  73. $notification->setContents($content);
  74. $notification->setIosBadgeType('Increase');
  75. $notification->setIosBadgeCount(1);
  76. $notification->setIsIos(true);
  77. $notification->setContentAvailable(true);
  78. $data['push_para']=$url;
  79. $data['push_type']=$type;
  80. $data['title']=$title;
  81. $data['message_id']=$one_push['id'];
  82. $notification->setData($data);
  83. $notification->setMutableContent(true);
  84. $apns_alert['title']=$title;
  85. $notification->setApnsAlert($apns_alert);
  86. if($one_push['template_image']){
  87. $request = Request::instance();
  88. $domain=$request->domain();
  89. $images['id'] = $domain.$one_push['template_image'];
  90. $notification->setIosAttachments($images);
  91. }
  92. return $notification;
  93. }
  94. public function sendTest($one_push){
  95. $appId =$this->getAppId();
  96. $notification = $this->createNotification($one_push);
  97. $notification->setAppId($appId);
  98. $notification->setIncludedSegments(['TestUsers']);
  99. return $this->apiInstance->createNotification($notification);
  100. }
  101. public function getAppId(){
  102. $url = $_SERVER['HTTP_HOST'];
  103. if (strpos($url, 'westkiss') !== false) {
  104. $appId ='6bbd561f-4d8e-4d04-a6c4-dbef1bf99694';
  105. }elseif (strpos($url, 'asteriahair') !== false) {
  106. $appId ='618fe580-bc97-4bf4-b2bb-5039f9dbbc82';
  107. }else{
  108. $appId ='b3124d44-7bc7-4965-95dc-0ecb502fdaea';
  109. }
  110. return $appId;
  111. }
  112. }