Test.php 3.0 KB

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