Overview

Namespaces

  • NNV
    • OneSignal
      • API
      • Constants
      • Utils

Classes

  • NNV\OneSignal\API\App
  • NNV\OneSignal\API\Notification
  • NNV\OneSignal\API\Player
  • NNV\OneSignal\Constants\DeviceTypes
  • NNV\OneSignal\Constants\NotificationTypes
  • NNV\OneSignal\Constants\TestTypes
  • NNV\OneSignal\OneSignal
  • NNV\OneSignal\Utils\Validation
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace NNV\OneSignal\API;
  4: 
  5: use NNV\OneSignal\OneSignal;
  6: use NNV\OneSignal\Utils\Validation;
  7: use Symfony\Component\OptionsResolver\Options;
  8: 
  9: /**
 10:  * Notification API
 11:  */
 12: class Notification
 13: {
 14:     /**
 15:      * OneSignal instance
 16:      *
 17:      * @var \NNV\OneSignal\OneSignal
 18:      */
 19:     private $oneSignal;
 20: 
 21:     /**
 22:      * Application ID
 23:      *
 24:      * @var string
 25:      */
 26:     private $appIDKey;
 27: 
 28:     /**
 29:      * RESTful API Key
 30:      *
 31:      * @var string
 32:      */
 33:     private $restAPIKey;
 34: 
 35:     /**
 36:      * Extra options
 37:      *
 38:      * @var array
 39:      */
 40:     private $extraOptions;
 41: 
 42:     /**
 43:      * @param \NNV\OneSignal\OneSignal $oneSignal OneSignal instance
 44:      * @param string $appIDKey Application ID
 45:      * @param string $restAPIKey API Key for REST JSON API
 46:      */
 47:     public function __construct(OneSignal $oneSignal, $appIDKey = null, $restAPIKey = null)
 48:     {
 49:         $this->oneSignal    = $oneSignal;
 50:         $this->appIDKey     = ($appIDKey ? $appIDKey : $oneSignal->getAppIDKey());
 51:         $this->restAPIKey   = ($restAPIKey ? $restAPIKey : $oneSignal->getRESTAPIKey());
 52:         $this->extraOptions = [
 53:             'headers' => [
 54:                 'Authorization' => sprintf('Basic %s', $this->restAPIKey),
 55:             ]
 56:         ];
 57:     }
 58: 
 59:     /**
 60:      * Sends notifications to users
 61:      *
 62:      * @param  array  $notificationData Notification data
 63:      * @return \NNV\OneSignal\OneSignal::execute()
 64:      */
 65:     public function create(array $notificationData)
 66:     {
 67:         $notificationData = array_merge($notificationData, [
 68:             'app_id' => $this->appIDKey,
 69:         ]);
 70:         $notificationData = $this->validateNotificationData(
 71:             $notificationData,
 72:             $this->getNotificationDataRules()
 73:         );
 74: 
 75:         return $this->oneSignal->execute('notifications', 'POST', array_merge([
 76:             'headers' => [
 77:                 'Content-Type' => 'application/json',
 78:             ],
 79:             'json' => $notificationData,
 80:         ], $this->extraOptions));
 81:     }
 82: 
 83:     /**
 84:      * Stop a scheduled or currently outgoing notification
 85:      *
 86:      * @param  string $notificationID Notification ID
 87:      * @return \NNV\OneSignal\OneSignal::execute()
 88:      */
 89:     public function cancel($notificationID)
 90:     {
 91:         $url = sprintf('notifications/%s?app_id=%s', $notificationID, $this->appIDKey);
 92: 
 93:         return $this->oneSignal->execute($url, 'DELETE', $this->extraOptions);
 94:     }
 95: 
 96:     /**
 97:      * View the details of a single notification
 98:      *
 99:      * @param  string $notificationID Notification ID
100:      * @return \NNV\OneSignal\OneSignal::execute()
101:      */
102:     public function get($notificationID)
103:     {
104:         $url = sprintf('notifications/%s?app_id=%s', $notificationID, $this->appIDKey);
105: 
106:         return $this->oneSignal->execute($url, 'GET', $this->extraOptions);
107:     }
108: 
109:     /**
110:      * View the details of multiple notifications
111:      *
112:      * @param  integer $limit  How many notifications to return.
113:      * @param  integer $offset Result offset. Result are sorted by queued_at
114:      * @return \NNV\OneSignal\OneSignal::execute()
115:      */
116:     public function all($limit = 10, $offset = 0)
117:     {
118:         $url = sprintf(
119:             'notifications?app_id=%s&limit=%s&offset=%s',
120:             $this->appIDKey,
121:             $limit,
122:             $offset
123:         );
124: 
125:         return $this->oneSignal->execute($url, 'GET', $this->extraOptions);
126:     }
127: 
128:     /**
129:      * Track when users open a notification
130:      *
131:      * @param  string $notificationID Notification ID
132:      * @return \NNV\OneSignal\OneSignal::execute()
133:      */
134:     public function trackOpen($notificationID)
135:     {
136:         $url = sprintf('notifications/%s', $notificationID);
137:         $trackOpenData = [
138:             'app_id' => $this->appIDKey,
139:             'opened' => true,
140:         ];
141: 
142:         return $this->oneSignal->execute($url, 'PUT', array_merge([
143:             'form_params' => $trackOpenData,
144:         ], $this->extraOptions));
145:     }
146: 
147:     /**
148:      * Notification data validation rules
149:      *
150:      * @return array Validation rules
151:      */
152:     private function getNotificationDataRules()
153:     {
154:         return [
155:             'required' => ['included_segments', 'app_id'],
156:             'defined' => [
157:                 'app_id' => 'string',
158:                 'app_ids' => 'array',
159:                 'included_segments' => 'array',
160:                 'excluded_segments' => 'array',
161:                 'filters' => 'array',
162:                 'include_player_ids' => 'array',
163:                 'include_ios_tokens' => 'array',
164:                 'include_wp_urls' => 'array',
165:                 'include_wp_wns_uris' => 'array',
166:                 'include_amazon_reg_ids' => 'array',
167:                 'include_chrome_reg_ids' => 'array',
168:                 'include_chrome_web_reg_ids' => 'array',
169:                 'include_android_reg_ids' => 'array',
170:                 'contents' => 'array',
171:                 'headings' => 'array',
172:                 'subtitle' => 'array',
173:                 'template_id' => 'string',
174:                 'content_available' => 'bool',
175:                 'mutable_content' => 'bool',
176:                 'data' => 'array',
177:                 'url' => 'string',
178:                 'ios_attachments' => 'array',
179:                 'big_picture' => 'string',
180:                 'adm_big_picture' => 'string',
181:                 'chrome_big_picture' => 'string',
182:                 'buttons' => 'array',
183:                 'web_buttons' => 'array',
184:                 'ios_category' => 'string',
185:                 'android_background_layout' => 'array',
186:                 'small_icon' => 'string',
187:                 'large_icon' => 'string',
188:                 'chrome_web_icon' => 'string',
189:                 'firefox_icon' => 'string',
190:                 'adm_small_icon' => 'string',
191:                 'adm_large_icon' => 'string',
192:                 'chrome_icon' => 'string',
193:                 'ios_sound' => 'string',
194:                 'android_sound' => 'string',
195:                 'adm_sound' => 'string',
196:                 'wp_sound' => 'string',
197:                 'wp_wns_sound' => 'string',
198:                 'android_led_color' => 'string',
199:                 'android_accent_color' => 'string',
200:                 'android_visibility' => [
201:                     'allowedTypes' => 'int',
202:                     'allowedValues' => [1, 0, -1],
203:                 ],
204:                 'ios_badgeType' => [
205:                     'allowedTypes' => 'string',
206:                     'allowedValues' => ['None', 'SetTo', 'Increase'],
207:                 ],
208:                 'ios_badgeCount' => 'string',
209:                 'collapse_id' => 'string',
210:                 'send_after' => 'string',
211:                 'delayed_option' => 'string',
212:                 'delivery_time_of_day' => 'string',
213:                 'ttl' => 'int',
214:                 'priority' => 'int',
215:                 'android_group' => 'string',
216:                 'android_group_message' => 'array',
217:                 'adm_group' => 'string',
218:                 'adm_group_message' => 'array',
219:                 'isIos' => 'bool',
220:                 'isAndroid' => 'bool',
221:                 'isAnyWeb' => 'bool',
222:                 'isChromeWeb' => 'bool',
223:                 'isFirefox' => 'bool',
224:                 'isSafari' => 'bool',
225:                 'isWP' => 'bool',
226:                 'isWP_WNS' => 'bool',
227:                 'isAdm' => 'bool',
228:                 'isChrome' => 'bool',
229:             ],
230:         ];
231:     }
232: 
233:     /**
234:      * Validate notification data
235:      *
236:      * @param  array  $notificationData      Notification data
237:      * @param  array  $notificationDataRules Notification data validation rules
238:      * @return mixed  NNV\OneSignal\Utils\Validation::validate or data resolved
239:      */
240:     private function validateNotificationData(array $notificationData, array $notificationDataRules)
241:     {
242:         $validation = new Validation;
243:         $isObjectOptions = [
244:             'contents', 'headings', 'subtitle', 'data',
245:             'ios_attachments', 'android_background_layout',
246:             'android_group_message', 'adm_group_message', 'buttons',
247:             'web_buttons', 'filters',
248:         ];
249: 
250:         $validation->setMultiRequired($notificationDataRules['required'])
251:                    ->setMultiDefined($notificationDataRules['defined'])
252:                    ->setAllowedValues('send_after', function($sendAfter) {
253:                         try {
254:                             new \DateTime($sendAfter);
255:                         } catch (\Exception $e) {
256:                             return false;
257:                         }
258: 
259:                         return true;
260:                    })
261:                    ->setAllowedValues('delivery_time_of_day', function($deliveryTimeOfDay) {
262:                         return preg_match('/^(0?\d|1[0-2]):[0-5]\d(am|pm)$/i', $deliveryTimeOfDay);
263:                    });
264: 
265:         foreach ($isObjectOptions as $objectOption) {
266:             $validation->setNormalizer($objectOption, function(Options $options, $objectValue) {
267:                 return json_decode(json_encode($objectValue));
268:             });
269:         }
270: 
271:         return $validation->validate($notificationData);
272:     }
273: }
274: 
API documentation generated by ApiGen