Message.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. class Message extends Api
  24. {
  25. protected $noNeedLogin = ['*'];
  26. protected $noNeedRight = ['*'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $config= Configuration::getDefaultConfiguration()
  31. ->setAppKeyToken('MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh')
  32. ->setUserKeyToken('YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh');
  33. $this->apiInstance = new DefaultApi(
  34. new GuzzleHttp\Client(),
  35. $config
  36. );
  37. }
  38. public function getPlayers()
  39. {
  40. $limit = 10;
  41. $getPlayersResult = $this->apiInstance->getPlayers('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694', $limit);
  42. print_r($getPlayersResult->getPlayers());
  43. }
  44. public function addPlayers()
  45. {
  46. $data=$this->request->post();
  47. $user = User::getByUserId($data['user_id']);
  48. if ($user) {
  49. $user->group_id = $data['group_id'];
  50. $user->save();
  51. }else{
  52. User::create($data, true);
  53. }
  54. $this->success();
  55. }
  56. function createNotification($enContent){
  57. $content = new StringMap();
  58. $content->setEn($enContent);
  59. $notification = new Notification();
  60. $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694');
  61. $notification->setContents($content);
  62. $notification->setIncludedSegments(['SegmentA']);
  63. return $notification;
  64. }
  65. public function send(){
  66. $notification = $this->createNotification(' wk Test notification');
  67. $result = $this->apiInstance->createNotification($notification);
  68. print_r($result);
  69. }
  70. }