UNPKG

@ua/react-native-airship

Version:
257 lines (233 loc) 7.02 kB
"use strict"; import { NativeEventEmitter, Platform } from 'react-native'; /** * Airship Push. */ export class AirshipPush { /** * iOS only push methods. */ /** * Android only push methods. */ constructor(module) { this.module = module; this.iOS = new AirshipPushIOS(module); this.android = new AirshipPushAndroid(module); } /** * Enables/disables notifications on Airship. * * When enabled, it will cause the user to be prompted for * the permission on platforms that support it. * To get the result of the prompt, use `enableUserNotifications`. * @param enabled true to enable, false to disable * @returns A promise. */ setUserNotificationsEnabled(enabled) { return this.module.pushSetUserNotificationsEnabled(enabled); } /** * Checks if user notifications are enabled or not on Airship. * @returns A promise with the result. */ isUserNotificationsEnabled() { return this.module.pushIsUserNotificationsEnabled(); } /** * Enables user notifications. * @param options Optional options. * @returns A promise with the permission result. */ enableUserNotifications(options) { return this.module.pushEnableUserNotifications(options); } /** * Gets the notification status. * @returns A promise with the result. */ getNotificationStatus() { return this.module.pushGetNotificationStatus(); } /** * Gets the registration token if generated. * Use the event EventType.PushTokenReceived to be notified * when available. * @returns A promise with the result. */ getRegistrationToken() { return this.module.pushGetRegistrationToken(); } /** * Gets the list of active notifications. * * On Android, this list only includes notifications * sent through Airship. * @returns A promise with the result. */ getActiveNotifications() { return this.module.pushGetActiveNotifications(); } /** * Clears all notifications for the app. */ clearNotifications() { return this.module.pushClearNotifications(); } /** * Clears a specific notification. * * On Android, you can use this method to clear * notifications outside of Airship, The identifier is in * the format of <tag>:<id>. * @param identifier The identifier. */ clearNotification(identifier) { return this.module.pushClearNotification(identifier); } } /** * iOS Push. */ export class AirshipPushIOS { constructor(module) { this.module = module; this.eventEmitter = new NativeEventEmitter(module); if (Platform.OS === 'ios') { this.eventEmitter.addListener("com.airship.ios.override_presentation_options", event => { let payload = event["pushPayload"]; let requestId = event["requestId"]; if (this.presentationOverridesCallback) { this.presentationOverridesCallback(payload).then(result => { module.pushIosOverridePresentationOptions(requestId, result); }).catch(() => { module.pushIosOverridePresentationOptions(requestId, null); }); } }); } } /** * Overrides the presentation options per notification. If a `null` is returned, the default * options will be used. The callback should return as quickly as possible to avoid delaying notification delivery. * * @param callback A callback that can override the foreground presentation options. */ setForegroundPresentationOptionsCallback(callback) { if (Platform.OS === 'ios') { this.presentationOverridesCallback = callback; this.module.pushIosIsOverridePresentationOptionsEnabled(callback != null); } } /** * Sets the foreground presentation options. * @param options The foreground options. * @returns A promise. */ setForegroundPresentationOptions(options) { return this.module.pushIosSetForegroundPresentationOptions(options); } /** * Sets the notification options. * @param options The notification options. * @returns A promise. */ setNotificationOptions(options) { return this.module.pushIosSetNotificationOptions(options); } /** * Checks if autobadge is enabled. * @returns A promise with the result. */ isAutobadgeEnabled() { return this.module.pushIosIsAutobadgeEnabled(); } /** * Enables/disables autobadge. * @param enabled true to enable, false to disable. * @returns A promise. */ setAutobadgeEnabled(enabled) { return this.module.pushIosSetAutobadgeEnabled(enabled); } /** * Set the badge number. * @param badge The badge number. * @returns A promise. */ setBadgeNumber(badge) { return this.module.pushIosSetBadgeNumber(badge); } /** * Gets the badge number. * @returns A promise with the result. */ getBadgeNumber() { return this.module.pushIosGetBadgeNumber(); } /** * Gets the list of authorized notification settings. * @returns A promise with the result. */ getAuthorizedNotificationSettings() { return this.module.pushIosGetAuthorizedNotificationSettings(); } /** * Gets the authorized notification status. * @returns A promise with the result. */ getAuthorizedNotificationStatus() { return this.module.pushIosGetAuthorizedNotificationStatus(); } } /** * Android Push. */ export class AirshipPushAndroid { constructor(module) { this.module = module; this.eventEmitter = new NativeEventEmitter(module); if (Platform.OS === 'android') { this.eventEmitter.addListener("com.airship.android.override_foreground_display", event => { let payload = event["pushPayload"]; let requestId = event["requestId"]; if (this.foregroundDisplayPredicate) { this.foregroundDisplayPredicate(payload).then(result => { module.pushAndroidOverrideForegroundDisplay(requestId, result); }).catch(() => { module.pushAndroidOverrideForegroundDisplay(requestId, true); }); } }); } } /** * Checks if a notification category/channel is enabled. * @param channel The channel name. * @returns A promise with the result. */ isNotificationChannelEnabled(channel) { return this.module.pushAndroidIsNotificationChannelEnabled(channel); } /** * Sets the notification config. * @param config The notification config. */ setNotificationConfig(config) { return this.module.pushAndroidSetNotificationConfig(config); } /** * Overrides the foreground display per notification. * * The predicate should return as quickly as possible to avoid delaying notification delivery. * * @param predicate A foreground display predicate. */ setForegroundDisplayPredicate(predicate) { if (Platform.OS === 'android') { this.foregroundDisplayPredicate = predicate; this.module.pushAndroidIsOverrideForegroundDisplayEnabled(predicate != null); } } } //# sourceMappingURL=AirshipPush.js.map