1
+ <?php
2
+
3
+
4
+ /**
5
+ * Firebase Notification Class
6
+ *
7
+ * @category Firebase FCM Notification
8
+ * @package FCM
9
+ * @author Emmanuel O. Sobande <[email protected] >
10
+ * @copyright Copyright (c) 2017
11
+ * @link http://github.com/eosobande/php-firebase-class
12
+ * @version 1.0
13
+ */
14
+
15
+ class FCM {
16
+
17
+ const CONTENT_JSON = 'application/json ' ;
18
+ const CONTENT_TEXT = 'application/x-www-form-urlencoded;charset=UTF-8 ' ;
19
+
20
+ const ERROR = 'error ' ;
21
+ const SUCCESS = 'success ' ;
22
+ const FAILURE = 'failure ' ;
23
+ const CANONICAL_IDS = 'canonical_ids ' ;
24
+ const REGISTRATION_ID = 'registration_id ' ;
25
+ const REGISTRATION_IDS = 'registration_ids ' ;
26
+ const RESULTS = 'results ' ;
27
+
28
+ const UNAVAILABLE = 'Unavailable ' ;
29
+ const INVALID = 'InvalidRegistration ' ;
30
+ const NOT_REGISTERED = 'NotRegistered ' ;
31
+ const EXCEEDED = 'TopicsMessageRateExceeded ' ;
32
+
33
+ private $ header ;
34
+ private $ content_type ;
35
+ private $ message ;
36
+ private $ response ;
37
+ private $ token ;
38
+ private $ time_to_live ;
39
+ private $ collapse_key ;
40
+
41
+ function __construct ($ content_type , $ time_to_live , $ collapse_key ) {
42
+
43
+ $ this ->content_type = $ content_type ;
44
+ $ this ->time_to_live = (int ) $ time_to_live ;
45
+ $ this ->collapse_key = $ collapse_key ;
46
+ $ this ->headers ();
47
+
48
+ }
49
+
50
+ private function headers () {
51
+
52
+ $ this ->headers = [
53
+
54
+ 'Content-Type: ' . $ this ->content_type ,
55
+ 'Authorization:key= ' . FB_API_KEY
56
+
57
+ ];
58
+
59
+ }
60
+
61
+ public function topics ($ to , $ condition , $ body =null , $ data =null , $ title =null ) {
62
+
63
+ if ($ to != null ) {
64
+ $ this ->message ['to ' ] = '/topics/ ' . $ to ;
65
+ } elseif ($ condition != null ) {
66
+ $ this ->message ['condition ' ] = $ condition ;
67
+ }
68
+
69
+ $ this ->message ($ body , $ data , $ title );
70
+
71
+ }
72
+
73
+ public function notification ($ token , $ body , $ data =null , $ title =null ) {
74
+
75
+ $ this ->token = $ token ;
76
+ $ this ->message [self ::REGISTRATION_IDS ] = $ this ->token ;
77
+ $ this ->message ($ body , $ data , $ title );
78
+
79
+ }
80
+
81
+ private function message ($ body , $ data , $ title ) {
82
+
83
+ if ($ body != null ) {
84
+ $ this ->message ['notification ' ] = ['title ' => $ title ? $ title : APP_NAME , 'body ' => $ body ];
85
+ }
86
+
87
+ if ($ data != null ) {
88
+ $ this ->message ['data ' ] = $ data ;
89
+ }
90
+
91
+ if ($ this ->time_to_live != null ) {
92
+ $ this ->message ['time_to_live ' ] = $ this ->time_to_live ;
93
+ }
94
+
95
+ if ($ this ->collapse_key != null ) {
96
+ $ this ->message ['collapse_key ' ] = $ this ->collapse_key ;
97
+ }
98
+ $ this ->send ();
99
+
100
+ }
101
+
102
+ private function response () {
103
+
104
+ if ((isset ($ this ->response [self ::FAILURE ]) && $ this ->response [self ::FAILURE ] > 0 ) ||
105
+ (isset ($ this ->response [self ::CANONICAL_IDS ]) && $ this ->response [self ::CANONICAL_IDS ] > 0 )) {
106
+
107
+ foreach ($ this ->response [self ::RESULTS ] as $ key => $ result ) {
108
+
109
+ if (isset ($ result [self ::ERROR ])) {
110
+
111
+ switch ($ result [self ::ERROR ]) {
112
+
113
+ case self ::UNAVAILABLE :
114
+ // do something
115
+
116
+ break ;
117
+
118
+ case self ::INVALID :
119
+ // do something
120
+
121
+ unset($ this ->token [$ key ]);
122
+ break ;
123
+
124
+ case self ::NOT_REGISTERED :
125
+ $ this ->remove_registration_id ($ this ->token [$ key ]);
126
+ unset($ this ->token [$ key ]);
127
+ break ;
128
+
129
+ case self ::EXCEEDED :
130
+ // do something
131
+ break ;
132
+
133
+ }
134
+
135
+ } elseif (isset ($ result [self ::REGISTRATION_ID ])) {
136
+ $ this ->update_registration_id ($ this ->token [$ key ], $ result [self ::REGISTRATION_ID ]);
137
+ unset($ this ->token [$ key ]);
138
+ } else {
139
+ unset($ this ->token [$ key ]);
140
+ }
141
+ }
142
+
143
+ $ this ->send (); // resending
144
+ }
145
+
146
+ }
147
+
148
+ private function remove_registration_id ($ token ) {
149
+ // do something
150
+ }
151
+
152
+ private function update_registration_id ($ old , $ new ) {
153
+ // do something
154
+ }
155
+
156
+ private function send () {
157
+
158
+ if ($ this ->message != null ) {
159
+
160
+ $ ch = curl_init ();
161
+ curl_setopt_array ($ ch , [
162
+
163
+ CURLOPT_URL => FB_URL ,
164
+ CURLOPT_HTTPHEADER => $ this ->headers ,
165
+ CURLOPT_POST => true ,
166
+ CURLOPT_RETURNTRANSFER => true ,
167
+ CURLOPT_SSL_VERIFYPEER => false ,
168
+ CURLOPT_POSTFIELDS => json_encode ($ this ->message )
169
+
170
+ ]);
171
+
172
+ $ this ->response = json_decode (curl_exec ($ ch ), true );
173
+ curl_close ($ ch );
174
+ $ this ->response ();
175
+ }
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+
0 commit comments