'use strict' ; import { NativeModules , Platform } from 'react-native' ; import EventManager from './events/EventManager' ; import { PERMISSION_CHANGED , SUBSCRIPTION_CHANGED , IN_APP_MESSAGE_CLICKED , NOTIFICATION_WILL_SHOW , NOTIFICATION_OPENED , EMAIL_SUBSCRIPTION_CHANGED , SMS_SUBSCRIPTION_CHANGED , IN_APP_MESSAGE_WILL_DISPLAY , IN_APP_MESSAGE_WILL_DISMISS , IN_APP_MESSAGE_DID_DISMISS , IN_APP_MESSAGE_DID_DISPLAY , } from './events/events' ; import { DeviceState , ChangeEvent , PermissionChange , SubscriptionChange , EmailSubscriptionChange , SMSSubscriptionChange , } from './models/Subscription' ; import NotificationReceivedEvent from './events/NotificationReceivedEvent' ; import { OpenedEvent } from './models/NotificationEvents' ; import { OutcomeEvent } from './models/Outcomes' ; import { InAppMessage , InAppMessageAction , InAppMessageLifecycleHandlerObject } from './models/InAppMessage' ; import { isValidCallback , isNativeModuleLoaded } from './helpers' ; const RNOneSignal = NativeModules . OneSignal ; const eventManager = new EventManager ( RNOneSignal ) ; export type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 ; export default class OneSignal { param { string } appId returns static setAppId ( appId ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . setAppId ( appId ) ; } param { ( event : ChangeEvent < PermissionChange > ) => void } observer returns static addPermissionObserver ( observer ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( observer ) ; RNOneSignal . addPermissionObserver ( ) ; eventManager . addEventHandler < PermissionChange > ( PERMISSION_CHANGED , observer ) ; } returns static clearPermissionObservers ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removePermissionObserver ( ) ; eventManager . clearEventHandler ( PERMISSION_CHANGED ) ; } param { ( event : ChangeEvent < SubscriptionChange > ) => void } observer returns static addSubscriptionObserver ( observer ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( observer ) ; RNOneSignal . addSubscriptionObserver ( ) ; eventManager . addEventHandler < SubscriptionChange > ( SUBSCRIPTION_CHANGED , observer ) ; } returns static clearSubscriptionObservers ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removeSubscriptionObserver ( ) ; eventManager . clearEventHandler ( SUBSCRIPTION_CHANGED ) ; } param { ( event : ChangeEvent < EmailSubscriptionChange > ) => void } observer returns static addEmailSubscriptionObserver ( observer ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( observer ) ; RNOneSignal . addEmailSubscriptionObserver ( ) ; eventManager . addEventHandler < EmailSubscriptionChange > ( EMAIL_SUBSCRIPTION_CHANGED , observer ) ; } returns static clearEmailSubscriptionObservers ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removeEmailSubscriptionObserver ( ) ; eventManager . clearEventHandler ( EMAIL_SUBSCRIPTION_CHANGED ) ; } param { ( event : ChangeEvent < SMSSubscriptionChange > ) => void } observer returns static addSMSSubscriptionObserver ( observer ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( observer ) ; RNOneSignal . addSMSSubscriptionObserver ( ) ; eventManager . addEventHandler < SMSSubscriptionChange > ( SMS_SUBSCRIPTION_CHANGED , observer ) ; } returns static clearSMSSubscriptionObservers ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removeSMSSubscriptionObserver ( ) ; eventManager . clearEventHandler ( SMS_SUBSCRIPTION_CHANGED ) ; } param { ( event : NotificationReceivedEvent ) => void } handler static setNotificationWillShowInForegroundHandler ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( handler ) ; RNOneSignal . setNotificationWillShowInForegroundHandler ( ) ; eventManager . setEventHandler < NotificationReceivedEvent > ( NOTIFICATION_WILL_SHOW , handler ) ; } param { ( openedEvent : OpenedEvent ) => void } handler returns static setNotificationOpenedHandler ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( handler ) ; RNOneSignal . setNotificationOpenedHandler ( ) ; eventManager . setEventHandler < OpenedEvent > ( NOTIFICATION_OPENED , handler ) ; } param { ( response : boolean ) => void } handler returns static promptForPushNotificationsWithUserResponse ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'ios' ) { isValidCallback ( handler ) ; RNOneSignal . promptForPushNotificationsWithUserResponse ( handler ) ; } else { console . log ( "promptForPushNotificationsWithUserResponse: this function is not supported on Android" ) ; } } param { ( response : boolean ) => void } handler returns static registerForProvisionalAuthorization ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'ios' ) { isValidCallback ( handler ) ; RNOneSignal . registerForProvisionalAuthorization ( handler ) ; } else { console . log ( "registerForProvisionalAuthorization: this function is not supported on Android" ) ; } } param { boolean } disable returns static disablePush ( disable ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . disablePush ( disable ) ; } param { boolean } unsubscribe returns static unsubscribeWhenNotificationsAreDisabled ( unsubscribe ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'android' ) { RNOneSignal . unsubscribeWhenNotificationsAreDisabled ( unsubscribe ) ; } else { console . log ( "unsubscribeWhenNotificationsAreDisabled: this function is not supported on iOS" ) ; } } returns static isLocationShared ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return Promise . resolve ( false ) ; return RNOneSignal . isLocationShared ( ) ; } param { boolean } shared returns static setLocationShared ( shared ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . setLocationShared ( shared ) ; } returns static promptLocation ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . promptLocation ( ) ; } returns static async getDeviceState ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return Promise . resolve ( null ) ; const deviceState = await RNOneSignal . getDeviceState ( ) ; if ( Platform . OS === 'android' ) { deviceState [ 'hasNotificationPermission' ] = deviceState [ 'areNotificationsEnabled' ] ; delete deviceState [ 'areNotificationsEnabled' ] ; } return deviceState ; } param { string } language returns static setLanguage ( language ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . setLanguage ( language ) ; } param { string } key param { string } value returns static sendTag ( key , value ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! key || ( ! value && value !== "" ) ) { console . error ( "OneSignal: sendTag: must include a key and a value" ) ; } RNOneSignal . sendTag ( key , value ) ; } param { [ key : string ] returns static sendTags ( tags ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; let keys = Object . keys ( tags ) ; if ( keys . length === 0 ) { console . error ( `OneSignal: sendTags: argument must be of type object of the form { key : 'value' }` ) ; } RNOneSignal . sendTags ( tags || { } ) ; } param { ( tags : { [ key : string ] : string } ) => void } handler returns static getTags ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . getTags ( handler ) ; } param { string } key returns static deleteTag ( key ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( typeof key !== "string" ) { console . error ( "OneSignal: deleteTag: key argument must be of type string" ) ; } RNOneSignal . deleteTags ( [ key ] ) ; } param { string [ ] } keys static deleteTags ( keys ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! Array . isArray ( keys ) ) { console . error ( "OneSignal: deleteTags: argument must be of array type" ) ; } RNOneSignal . deleteTags ( keys ) ; } param { string } email param { string } emailAuthCode param { Function } handler returns static setEmail ( email , emailAuthCode ? , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( emailAuthCode === undefined ) emailAuthCode = null ; if ( ! handler && Platform . OS === 'ios' ) handler = function ( ) { } ; RNOneSignal . setEmail ( email , emailAuthCode , handler ) ; } param { Function } handler static logoutEmail ( handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! handler && Platform . OS === 'ios' ) handler = function ( ) { } ; RNOneSignal . logoutEmail ( handler ) ; } param { string } smsNumber param { string } smsAuthCode param { Function } handler returns static setSMSNumber ( smsNumber , smsAuthCode ? , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( smsAuthCode === undefined ) smsAuthCode = null ; if ( ! handler && Platform . OS === 'ios' ) handler = function ( ) { } ; RNOneSignal . setSMSNumber ( smsNumber , smsAuthCode , handler ) ; } param { Function } handler static logoutSMSNumber ( handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! handler && Platform . OS === 'ios' ) handler = function ( ) { } ; RNOneSignal . logoutSMSNumber ( handler ) ; } param { string } notificationObjectString param { ( success : object ) => void } onSuccess param { ( failure : object ) => void } onFailure returns static postNotification ( notificationObjectString , onSuccess ? , onFailure ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! onSuccess ) onSuccess = function ( ) { } ; if ( ! onFailure ) onFailure = function ( ) { } ; RNOneSignal . postNotification ( notificationObjectString , onSuccess , onFailure ) ; } returns static clearOneSignalNotifications ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'android' ) { RNOneSignal . clearOneSignalNotifications ( ) ; } else { console . log ( "clearOneSignalNotifications: this function is not supported on iOS" ) ; } } param { number } id returns static removeNotification ( id ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'android' ) { RNOneSignal . removeNotification ( id ) ; } else { console . log ( "removeNotification: this function is not supported on iOS" ) ; } } param { string } id returns static removeGroupedNotifications ( id ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( Platform . OS === 'android' ) { RNOneSignal . removeGroupedNotifications ( id ) ; } else { console . log ( "removeGroupedNotifications: this function is not supported on iOS" ) ; } } param { string } externalId param { string } externalIdAuthCode param { ( results : object ) => void } handler returns static setExternalUserId ( externalId , handlerOrAuth ? , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( typeof handlerOrAuth === "function" ) { RNOneSignal . setExternalUserId ( externalId , null , handlerOrAuth ) ; return ; } if ( ! handler && Platform . OS === 'ios' ) { handler = function ( ) { } ; } RNOneSignal . setExternalUserId ( externalId , handlerOrAuth , handler ) ; } param { ( results : object ) => void } handler returns static removeExternalUserId ( handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( handler === undefined && Platform . OS === 'ios' ) handler = function ( ) { } ; RNOneSignal . removeExternalUserId ( handler ) ; } param { ( action : InAppMessageAction ) => void } handler returns static setInAppMessageClickHandler ( handler ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; isValidCallback ( handler ) ; RNOneSignal . initInAppMessageClickHandlerParams ( ) ; RNOneSignal . setInAppMessageClickHandler ( ) ; eventManager . setEventHandler < InAppMessageAction > ( IN_APP_MESSAGE_CLICKED , handler ) ; } param { InAppMessageLifecycleHandlerObject } handlerObject returns static setInAppMessageLifecycleHandler ( handlerObject ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( handlerObject . onWillDisplayInAppMessage ) { isValidCallback ( handlerObject . onWillDisplayInAppMessage ) ; eventManager . setEventHandler < InAppMessage > ( IN_APP_MESSAGE_WILL_DISPLAY , handlerObject . onWillDisplayInAppMessage ) ; } if ( handlerObject . onDidDisplayInAppMessage ) { isValidCallback ( handlerObject . onDidDisplayInAppMessage ) ; eventManager . setEventHandler < InAppMessage > ( IN_APP_MESSAGE_DID_DISPLAY , handlerObject . onDidDisplayInAppMessage ) ; } if ( handlerObject . onWillDismissInAppMessage ) { isValidCallback ( handlerObject . onWillDismissInAppMessage ) ; eventManager . setEventHandler < InAppMessage > ( IN_APP_MESSAGE_WILL_DISMISS , handlerObject . onWillDismissInAppMessage ) ; } if ( handlerObject . onDidDismissInAppMessage ) { isValidCallback ( handlerObject . onDidDismissInAppMessage ) ; eventManager . setEventHandler < InAppMessage > ( IN_APP_MESSAGE_DID_DISMISS , handlerObject . onDidDismissInAppMessage ) ; } RNOneSignal . setInAppMessageLifecycleHandler ( ) ; } param { string } key param { string | number | boolean } value returns static addTrigger ( key , value ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; if ( ! key || value == null ) { console . error ( "OneSignal: addTrigger: must include a key and a value" ) ; } let trigger = { } ; trigger [ key ] = value ; RNOneSignal . addTriggers ( trigger ) ; } param { [ key : string ] returns static addTriggers ( triggers ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; let keys = Object . keys ( triggers ) ; if ( keys . length === 0 ) { console . error ( `OneSignal: addTriggers: argument must be an object of the form { key : 'value' }` ) ; } RNOneSignal . addTriggers ( triggers ) ; } param { string [ ] } keys returns static removeTriggersForKeys ( keys ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removeTriggersForKeys ( keys ) ; } param { string } key returns static removeTriggerForKey ( key ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . removeTriggerForKey ( key ) ; } param { string } key returns static getTriggerValueForKey ( key ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return Promise . resolve ( null ) ; return RNOneSignal . getTriggerValueForKey ( key ) ; } param { boolean } pause returns static pauseInAppMessages ( pause ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . pauseInAppMessages ( pause ) ; } param { string } name param { ( event : OutcomeEvent ) => void } handler returns static sendOutcome ( name , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . sendOutcome ( name , handler ) ; } param { string } name param { ( event : OutcomeEvent ) => void } handler returns static sendUniqueOutcome ( name , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . sendUniqueOutcome ( name , handler ) ; } param { string } name param { string | number } value param { ( event : OutcomeEvent ) => void } handler returns static sendOutcomeWithValue ( name , value , handler ? ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . sendOutcomeWithValue ( name , Number ( value ) , handler ) ; } returns static userProvidedPrivacyConsent ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return Promise . resolve ( false ) ; return RNOneSignal . userProvidedPrivacyConsent ( ) ; } returns static requiresUserPrivacyConsent ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return Promise . resolve ( false ) ; return RNOneSignal . requiresUserPrivacyConsent ( ) ; } param { boolean } required returns static setRequiresUserPrivacyConsent ( required ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . setRequiresUserPrivacyConsent ( required ) ; } param { boolean } granted returns static provideUserConsent ( granted ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . provideUserConsent ( granted ) ; } param { LogLevel } nsLogLevel param { LogLevel } visualLogLevel returns static setLogLevel ( nsLogLevel , visualLogLevel ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; RNOneSignal . setLogLevel ( nsLogLevel , visualLogLevel ) ; } returns static clearHandlers ( ) { if ( ! isNativeModuleLoaded ( RNOneSignal ) ) return ; eventManager . clearHandlers ( ) ; } } export { ChangeEvent , PermissionChange , SubscriptionChange , EmailSubscriptionChange , SMSSubscriptionChange , NotificationReceivedEvent , OpenedEvent , InAppMessage , InAppMessageAction , InAppMessageLifecycleHandlerObject , OutcomeEvent , DeviceState , } ; export { default as OSNotification } from './OSNotification' ; export { OpenedEventAction , OpenedEventActionType } from './models/NotificationEvents' ; export { IosPermissionStatus , ObserverChangeEvent } from './models/Subscription' ;