@@ -6,47 +6,55 @@ import invariant from 'invariant';
66
77const RNOneSignal = NativeModules . OneSignal ;
88
9- var oneSignalEventEmitter = new NativeEventEmitter ( RNOneSignal ) ;
10-
119const eventBroadcastNames = [
1210 'OneSignal-remoteNotificationReceived' ,
1311 'OneSignal-remoteNotificationOpened' ,
1412 'OneSignal-idsAvailable' ,
1513 'OneSignal-emailSubscription'
1614] ;
1715
16+ var oneSignalEventEmitter ;
17+
1818var _eventNames = [ "received" , "opened" , "ids" , "emailSubscription" ] ;
1919
2020var _notificationHandler = new Map ( ) ;
2121var _notificationCache = new Map ( ) ;
2222var _listeners = [ ] ;
2323
24- for ( var i = 0 ; i < eventBroadcastNames . length ; i ++ ) {
25- var eventBroadcastName = eventBroadcastNames [ i ] ;
26- var eventName = _eventNames [ i ] ;
24+ if ( RNOneSignal != null ) {
25+ oneSignalEventEmitter = new NativeEventEmitter ( RNOneSignal ) ;
26+
27+ for ( var i = 0 ; i < eventBroadcastNames . length ; i ++ ) {
28+ var eventBroadcastName = eventBroadcastNames [ i ] ;
29+ var eventName = _eventNames [ i ] ;
2730
28- _listeners [ eventName ] = handleEventBroadcast ( eventName , eventBroadcastName )
31+ _listeners [ eventName ] = handleEventBroadcast ( eventName , eventBroadcastName )
32+ }
2933}
3034
3135function handleEventBroadcast ( type , broadcast ) {
32- return oneSignalEventEmitter . addListener (
33- broadcast , ( notification ) => {
36+ return oneSignalEventEmitter . addListener (
37+ broadcast , ( notification ) => {
3438 // Check if we have added listener for this type yet
3539 // Cache the result first if we have not.
3640 var handler = _notificationHandler . get ( type ) ;
3741
3842 if ( handler ) {
39- handler ( notification ) ;
43+ handler ( notification ) ;
4044 } else {
41- _notificationCache . set ( type , notification ) ;
45+ _notificationCache . set ( type , notification ) ;
4246 }
43- }
44- ) ;
47+ }
48+ ) ;
4549}
4650
47- export default class OneSignal {
51+ function checkIfInitialized ( ) {
52+ return RNOneSignal != null ;
53+ }
4854
55+ export default class OneSignal {
4956 static addEventListener ( type : any , handler : Function ) {
57+ if ( ! checkIfInitialized ( ) ) return ;
5058
5159 // Listen to events of notification received, opened, device registered and IDSAvailable.
5260
@@ -66,6 +74,8 @@ export default class OneSignal {
6674 }
6775
6876 static removeEventListener ( type , handler ) {
77+ if ( ! checkIfInitialized ( ) ) return ;
78+
6979 invariant (
7080 type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription' ,
7181 'OneSignal only supports `received`, `opened`, and `ids` events'
@@ -75,12 +85,16 @@ export default class OneSignal {
7585 }
7686
7787 static clearListeners ( ) {
88+ if ( ! checkIfInitialized ( ) ) return ;
89+
7890 for ( var i = 0 ; i < _eventNames . length ; i ++ ) {
7991 _listeners [ _eventNames ] . remove ( ) ;
8092 }
8193 }
8294
8395 static registerForPushNotifications ( ) {
96+ if ( ! checkIfInitialized ( ) ) return ;
97+
8498 if ( Platform . OS === 'ios' ) {
8599 RNOneSignal . registerForPushNotifications ( ) ;
86100 } else {
@@ -89,6 +103,8 @@ export default class OneSignal {
89103 }
90104
91105 static promptForPushNotificationsWithUserResponse ( callback : Function ) {
106+ if ( ! checkIfInitialized ( ) ) return ;
107+
92108 if ( Platform . OS === 'ios' ) {
93109 invariant (
94110 typeof callback === 'function' ,
@@ -101,6 +117,8 @@ export default class OneSignal {
101117 }
102118
103119 static requestPermissions ( permissions ) {
120+ if ( ! checkIfInitialized ( ) ) return ;
121+
104122 var requestedPermissions = { } ;
105123 if ( Platform . OS === 'ios' ) {
106124 if ( permissions ) {
@@ -123,6 +141,8 @@ export default class OneSignal {
123141 }
124142
125143 static configure ( ) {
144+ if ( ! checkIfInitialized ( ) ) return ;
145+
126146 RNOneSignal . configure ( ) ;
127147 }
128148
@@ -135,6 +155,8 @@ export default class OneSignal {
135155 }
136156
137157 static checkPermissions ( callback : Function ) {
158+ if ( ! checkIfInitialized ( ) ) return ;
159+
138160 if ( Platform . OS === 'ios' ) {
139161 invariant (
140162 typeof callback === 'function' ,
@@ -147,6 +169,8 @@ export default class OneSignal {
147169 }
148170
149171 static promptForPushNotificationPermissions ( callback ) {
172+ if ( ! checkIfInitialized ( ) ) return ;
173+
150174 if ( Platform . OS === 'ios' ) {
151175 RNOneSignal . promptForPushNotificationPermissions ( callback ) ;
152176 } else {
@@ -155,6 +179,8 @@ export default class OneSignal {
155179 }
156180
157181 static getPermissionSubscriptionState ( callback : Function ) {
182+ if ( ! checkIfInitialized ( ) ) return ;
183+
158184 invariant (
159185 typeof callback === 'function' ,
160186 'Must provide a valid callback'
@@ -163,22 +189,32 @@ export default class OneSignal {
163189 }
164190
165191 static sendTag ( key , value ) {
192+ if ( ! checkIfInitialized ( ) ) return ;
193+
166194 RNOneSignal . sendTag ( key , value ) ;
167195 }
168196
169197 static sendTags ( tags ) {
198+ if ( ! checkIfInitialized ( ) ) return ;
199+
170200 RNOneSignal . sendTags ( tags || { } ) ;
171201 }
172202
173203 static getTags ( next ) {
204+ if ( ! checkIfInitialized ( ) ) return ;
205+
174206 RNOneSignal . getTags ( next ) ;
175207 }
176208
177209 static deleteTag ( key ) {
210+ if ( ! checkIfInitialized ( ) ) return ;
211+
178212 RNOneSignal . deleteTag ( key ) ;
179213 }
180214
181215 static enableVibrate ( enable ) {
216+ if ( ! checkIfInitialized ( ) ) return ;
217+
182218 if ( Platform . OS === 'android' ) {
183219 RNOneSignal . enableVibrate ( enable ) ;
184220 } else {
@@ -187,6 +223,8 @@ export default class OneSignal {
187223 }
188224
189225 static enableSound ( enable ) {
226+ if ( ! checkIfInitialized ( ) ) return ;
227+
190228 if ( Platform . OS === 'android' ) {
191229 RNOneSignal . enableSound ( enable ) ;
192230 } else {
@@ -195,6 +233,8 @@ export default class OneSignal {
195233 }
196234
197235 static setEmail ( email , emailAuthCode , callback ) {
236+ if ( ! checkIfInitialized ( ) ) return ;
237+
198238 if ( emailAuthCode == undefined ) {
199239 //emailAuthCode is an optional parameter
200240 //since JS does not support function overloading,
@@ -211,6 +251,8 @@ export default class OneSignal {
211251 }
212252
213253 static logoutEmail ( callback ) {
254+ if ( ! checkIfInitialized ( ) ) return ;
255+
214256 invariant (
215257 typeof callback === 'function' ,
216258 'Must provide a valid callback'
@@ -220,19 +262,27 @@ export default class OneSignal {
220262 }
221263
222264 static setLocationShared ( shared ) {
265+ if ( ! checkIfInitialized ( ) ) return ;
266+
223267 RNOneSignal . setLocationShared ( shared ) ;
224268 }
225269
226270 static setSubscription ( enable ) {
271+ if ( ! checkIfInitialized ( ) ) return ;
272+
227273 RNOneSignal . setSubscription ( enable ) ;
228274 }
229275
230276 static promptLocation ( ) {
277+ if ( ! checkIfInitialized ( ) ) return ;
278+
231279 //Supported in both iOS & Android
232280 RNOneSignal . promptLocation ( ) ;
233281 }
234282
235283 static inFocusDisplaying ( displayOption ) {
284+ if ( ! checkIfInitialized ( ) ) return ;
285+
236286 if ( Platform . OS === 'android' ) {
237287 //Android: Set Display option of the notifications. displayOption is of type OSInFocusDisplayOption
238288 // 0 -> None, 1 -> InAppAlert, 2 -> Notification
@@ -244,6 +294,8 @@ export default class OneSignal {
244294 }
245295
246296 static postNotification ( contents , data , player_id , otherParameters ) {
297+ if ( ! checkIfInitialized ( ) ) return ;
298+
247299 if ( Platform . OS === 'android' ) {
248300 RNOneSignal . postNotification ( JSON . stringify ( contents ) , JSON . stringify ( data ) , player_id , JSON . stringify ( otherParameters ) ) ;
249301 } else {
@@ -252,6 +304,8 @@ export default class OneSignal {
252304 }
253305
254306 static clearOneSignalNotifications ( ) {
307+ if ( ! checkIfInitialized ( ) ) return ;
308+
255309 if ( Platform . OS === 'android' ) {
256310 RNOneSignal . clearOneSignalNotifications ( ) ;
257311 } else {
@@ -260,6 +314,8 @@ export default class OneSignal {
260314 }
261315
262316 static cancelNotification ( id ) {
317+ if ( ! checkIfInitialized ( ) ) return ;
318+
263319 if ( Platform . OS === 'android' ) {
264320 RNOneSignal . cancelNotification ( id ) ;
265321 } else {
@@ -269,22 +325,32 @@ export default class OneSignal {
269325
270326 //Sends MD5 and SHA1 hashes of the user's email address (https://documentation.onesignal.com/docs/ios-sdk-api#section-synchashedemail)
271327 static syncHashedEmail ( email ) {
328+ if ( ! checkIfInitialized ( ) ) return ;
329+
272330 RNOneSignal . syncHashedEmail ( email ) ;
273331 }
274332
275333 static setLogLevel ( nsLogLevel , visualLogLevel ) {
334+ if ( ! checkIfInitialized ( ) ) return ;
335+
276336 RNOneSignal . setLogLevel ( nsLogLevel , visualLogLevel ) ;
277337 }
278338
279339 static setRequiresUserPrivacyConsent ( required ) {
340+ if ( ! checkIfInitialized ( ) ) return ;
341+
280342 RNOneSignal . setRequiresUserPrivacyConsent ( required ) ;
281343 }
282344
283345 static provideUserConsent ( granted ) {
346+ if ( ! checkIfInitialized ( ) ) return ;
347+
284348 RNOneSignal . provideUserConsent ( granted ) ;
285349 }
286350
287351 static userProvidedPrivacyConsent ( ) {
352+ if ( ! checkIfInitialized ( ) ) return ;
353+
288354 //returns a promise
289355 return RNOneSignal . userProvidedPrivacyConsent ( ) ;
290356 }
0 commit comments