@@ -27,10 +27,83 @@ public function testCredentials() {
2727 return "APP ID: " .$ this ->appId ." REST: " .$ this ->restApiKey ;
2828 }
2929
30- public function sendNotification ( $ parameters = []) {
30+ private function requiresAuth () {
3131 $ this ->headers ['headers ' ]['Authorization ' ] = 'Basic ' .$ this ->restApiKey ;
32+ }
33+
34+ private function usesJSON () {
3235 $ this ->headers ['headers ' ]['Content-Type ' ] = 'application/json ' ;
36+ }
37+
38+ public function sendNotificationToUser ($ message , $ userId , $ url = null , $ data = null ) {
39+ $ contents = array (
40+ "en " => $ message
41+ );
42+
43+ $ params = array (
44+ 'app_id ' => $ this ->appId ,
45+ 'contents ' => $ contents ,
46+ 'include_player_ids ' => array ($ userId )
47+ );
48+
49+ if (isset ($ url )) {
50+ $ params ['url ' ] = $ url ;
51+ }
52+
53+ if (isset ($ data )) {
54+ $ params ['data ' ] = $ data ;
55+ }
56+
57+ $ this ->sendNotificationCustom ($ params );
58+ }
59+
60+ public function sendNotificationToAll ($ message , $ data = null ) {
61+ $ contents = array (
62+ "en " => $ message
63+ );
64+
65+ $ params = array (
66+ 'app_id ' => $ this ->appId ,
67+ 'contents ' => $ contents ,
68+ 'included_segments ' => array ('All ' )
69+ );
70+
71+ if (isset ($ data )) {
72+ $ params ['data ' ] = $ data ;
73+ }
74+
75+ $ this ->sendNotificationCustom ($ params );
76+ }
77+
78+ public function sendNotificationToSegment ($ message , $ segments , $ data = null ) {
79+ $ contents = array (
80+ "en " => $ message
81+ );
82+
83+ $ params = array (
84+ 'app_id ' => $ this ->appId ,
85+ 'contents ' => $ contents ,
86+ 'included_segments ' => array ('All ' )
87+ );
88+
89+ if (isset ($ data )) {
90+ $ params ['data ' ] = $ data ;
91+ }
92+
93+ $ this ->sendNotificationCustom ($ params );
94+ }
95+
96+ /**
97+ * Send a notification with custom parameters defined in
98+ * https://documentation.onesignal.com/v2.0/docs/notifications-create-notification
99+ * @param array $parameters
100+ * @return mixed
101+ */
102+ public function sendNotificationCustom ($ parameters = []){
103+ $ this ->requiresAuth ();
104+ $ this ->usesJSON ();
33105 $ this ->headers ['body ' ] = json_encode ($ parameters );
106+ $ this ->headers ['verify ' ] = false ;
34107 return $ this ->post ("notifications " );
35108 }
36109
0 commit comments