Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace common\components;
  5.  
  6.  
  7. class OneSignal
  8. {
  9.     const APP_API = '';
  10.     const REST_KEY = '';
  11.  
  12.     /**
  13.      * Отправка push пользователю
  14.      * @param string $pushId
  15.      * @param string $content
  16.      * @param string $title
  17.      * @param mixed $data дополнительные данные
  18.      * @return mixed
  19.      */
  20.     public static function sendMessage($pushId, $content, $title= '', $data = [])
  21.     {
  22.         $content = [
  23.             "en" => $content,
  24.             'ru' => $content
  25.         ];
  26.  
  27.         $fields = [
  28.             'app_id' => self::APP_API,
  29.             'data' => $data,
  30.             'contents' => $content,
  31.             'headings' => [
  32.                 "en" => $title,
  33.                 'ru' => $title
  34.             ],
  35.             'include_player_ids' => [$pushId],
  36.         ];
  37.         $fields = json_encode($fields);
  38.         $ch = curl_init();
  39.         curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
  40.         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
  41.             'Authorization: Basic ' . self::REST_KEY));
  42.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  43.         curl_setopt($ch, CURLOPT_HEADER, FALSE);
  44.         curl_setopt($ch, CURLOPT_POST, TRUE);
  45.         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  46.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  47.  
  48.         $response = curl_exec($ch);
  49.         curl_close($ch);
  50.         return $response;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement