Test.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }else{
  38. $appKeyToken='NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx';
  39. $userKeyToken='NWU3OGMxNzgtZjEzNS00M2JmLThmNzItZjY5YzIxOWU5YTQ3';
  40. }
  41. $config= Configuration::getDefaultConfiguration()
  42. ->setAppKeyToken($appKeyToken)
  43. ->setUserKeyToken($userKeyToken);
  44. $this->apiInstance = new DefaultApi(
  45. new GuzzleHttp\Client(),
  46. $config
  47. );
  48. }
  49. public function test()
  50. {
  51. $data =input();
  52. $id=$data['ids'];
  53. $wheres['id']=$id;
  54. $one_push = Db::name('message')->where($wheres)->find();
  55. if (empty($one_push)) {
  56. echo '没有查到推送队列';
  57. exit;
  58. }
  59. $result =$this->sendTest($one_push);
  60. $this->success('',$result);
  61. }
  62. function createNotification($one_push){
  63. $template_dsc =$one_push['template_dsc'];
  64. $title =$one_push['template_name'];
  65. $type =$one_push['type'];
  66. $url =$one_push['para'];
  67. $content = new StringMap();
  68. $content->setEn($template_dsc);
  69. $notification = new Notification();
  70. $notification->setContents($content);
  71. $notification->setIosBadgeType('Increase');
  72. $notification->setIosBadgeCount(1);
  73. $notification->setIsIos(true);
  74. $notification->setContentAvailable(true);
  75. $data['push_para']=$url;
  76. $data['push_type']=$type;
  77. $data['title']=$title;
  78. $data['message_id']=$one_push['id'];
  79. $notification->setData($data);
  80. $notification->setMutableContent(true);
  81. $apns_alert['title']=$title;
  82. $notification->setApnsAlert($apns_alert);
  83. if($one_push['template_image']){
  84. $request = Request::instance();
  85. $domain=$request->domain();
  86. $images['id'] = $domain.$one_push['template_image'];
  87. $notification->setIosAttachments($images);
  88. }
  89. return $notification;
  90. }
  91. public function sendTest($one_push){
  92. $appId =$this->getAppId();
  93. $notification = $this->createNotification($one_push);
  94. $notification->setAppId($appId);
  95. $notification->setIncludedSegments(['TestUsers']);
  96. $dt = new DateTime();
  97. $dt->modify('+1 hour');
  98. $notification->setSendAfter($dt);
  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. }else{
  106. $appId ='b3124d44-7bc7-4965-95dc-0ecb502fdaea';
  107. }
  108. return $appId;
  109. }
  110. }