UNPKG

react-native-firebase-compiled

Version:

A well tested, feature rich Firebase implementation for React Native, supporting iOS & Android. Individual module support for Admob, Analytics, Auth, Crash Reporting, Cloud Firestore, Database, Dynamic Links, Functions, Messaging (FCM), Remote Config, Sto

311 lines (238 loc) 9.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.statics = exports.default = exports.NAMESPACE = exports.MODULE_NAME = void 0; var _reactNative = require("react-native"); var _events = require("../../utils/events"); var _log = require("../../utils/log"); var _ModuleBase = _interopRequireDefault(require("../../utils/ModuleBase")); var _native = require("../../utils/native"); var _utils = require("../../utils"); var _AndroidAction = _interopRequireDefault(require("./AndroidAction")); var _AndroidChannel = _interopRequireDefault(require("./AndroidChannel")); var _AndroidChannelGroup = _interopRequireDefault(require("./AndroidChannelGroup")); var _AndroidNotifications = _interopRequireDefault(require("./AndroidNotifications")); var _IOSNotifications = _interopRequireDefault(require("./IOSNotifications")); var _AndroidRemoteInput = _interopRequireDefault(require("./AndroidRemoteInput")); var _Notification = _interopRequireDefault(require("./Notification")); var _types = require("./types"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const NATIVE_EVENTS = ['notifications_notification_displayed', 'notifications_notification_opened', 'notifications_notification_received']; const MODULE_NAME = 'RNFirebaseNotifications'; exports.MODULE_NAME = MODULE_NAME; const NAMESPACE = 'notifications'; // iOS 8/9 scheduling // fireDate: Date; // timeZone: TimeZone; // repeatInterval: NSCalendar.Unit; // repeatCalendar: Calendar; // region: CLRegion; // regionTriggersOnce: boolean; // iOS 10 scheduling // TODO // Android scheduling // TODO /** * @class Notifications */ exports.NAMESPACE = NAMESPACE; class Notifications extends _ModuleBase.default { constructor(app) { super(app, { events: NATIVE_EVENTS, hasCustomUrlSupport: false, moduleName: MODULE_NAME, hasMultiAppSupport: false, namespace: NAMESPACE }); _defineProperty(this, "_android", void 0); _defineProperty(this, "_ios", void 0); this._android = new _AndroidNotifications.default(this); this._ios = new _IOSNotifications.default(this); _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onNotificationDisplayed 'notifications_notification_displayed', notification => { _events.SharedEventEmitter.emit('onNotificationDisplayed', new _Notification.default(notification, this)); }); _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onNotificationOpened 'notifications_notification_opened', notificationOpen => { _events.SharedEventEmitter.emit('onNotificationOpened', { action: notificationOpen.action, notification: new _Notification.default(notificationOpen.notification, this), results: notificationOpen.results }); }); _events.SharedEventEmitter.addListener( // sub to internal native event - this fans out to // public event name: onNotification 'notifications_notification_received', notification => { _events.SharedEventEmitter.emit('onNotification', new _Notification.default(notification, this)); }); // Tell the native module that we're ready to receive events if (_reactNative.Platform.OS === 'ios') { (0, _native.getNativeModule)(this).jsInitialised(); } } get android() { return this._android; } get ios() { return this._ios; } /** * Cancel all notifications */ cancelAllNotifications() { return (0, _native.getNativeModule)(this).cancelAllNotifications(); } /** * Cancel a notification by id. * @param notificationId */ cancelNotification(notificationId) { if (!notificationId) { return Promise.reject(new Error('Notifications: cancelNotification expects a `notificationId`')); } return (0, _native.getNativeModule)(this).cancelNotification(notificationId); } /** * Display a notification * @param notification * @returns {*} */ displayNotification(notification) { if (!(notification instanceof _Notification.default)) { return Promise.reject(new Error(`Notifications:displayNotification expects a 'Notification' but got type ${typeof notification}`)); } try { return (0, _native.getNativeModule)(this).displayNotification(notification.build()); } catch (error) { return Promise.reject(error); } } getBadge() { return (0, _native.getNativeModule)(this).getBadge(); } getInitialNotification() { return (0, _native.getNativeModule)(this).getInitialNotification().then(notificationOpen => { if (notificationOpen) { return { action: notificationOpen.action, notification: new _Notification.default(notificationOpen.notification, this), results: notificationOpen.results }; } return null; }); } /** * Returns an array of all scheduled notifications * @returns {Promise.<Array>} */ getScheduledNotifications() { return (0, _native.getNativeModule)(this).getScheduledNotifications(); } onNotification(nextOrObserver) { let listener; if ((0, _utils.isFunction)(nextOrObserver)) { listener = nextOrObserver; } else if ((0, _utils.isObject)(nextOrObserver) && (0, _utils.isFunction)(nextOrObserver.next)) { listener = nextOrObserver.next; } else { throw new Error('Notifications.onNotification failed: First argument must be a function or observer object with a `next` function.'); } (0, _log.getLogger)(this).info('Creating onNotification listener'); _events.SharedEventEmitter.addListener('onNotification', listener); return () => { (0, _log.getLogger)(this).info('Removing onNotification listener'); _events.SharedEventEmitter.removeListener('onNotification', listener); }; } onNotificationDisplayed(nextOrObserver) { let listener; if ((0, _utils.isFunction)(nextOrObserver)) { listener = nextOrObserver; } else if ((0, _utils.isObject)(nextOrObserver) && (0, _utils.isFunction)(nextOrObserver.next)) { listener = nextOrObserver.next; } else { throw new Error('Notifications.onNotificationDisplayed failed: First argument must be a function or observer object with a `next` function.'); } (0, _log.getLogger)(this).info('Creating onNotificationDisplayed listener'); _events.SharedEventEmitter.addListener('onNotificationDisplayed', listener); return () => { (0, _log.getLogger)(this).info('Removing onNotificationDisplayed listener'); _events.SharedEventEmitter.removeListener('onNotificationDisplayed', listener); }; } onNotificationOpened(nextOrObserver) { let listener; if ((0, _utils.isFunction)(nextOrObserver)) { listener = nextOrObserver; } else if ((0, _utils.isObject)(nextOrObserver) && (0, _utils.isFunction)(nextOrObserver.next)) { listener = nextOrObserver.next; } else { throw new Error('Notifications.onNotificationOpened failed: First argument must be a function or observer object with a `next` function.'); } (0, _log.getLogger)(this).info('Creating onNotificationOpened listener'); _events.SharedEventEmitter.addListener('onNotificationOpened', listener); return () => { (0, _log.getLogger)(this).info('Removing onNotificationOpened listener'); _events.SharedEventEmitter.removeListener('onNotificationOpened', listener); }; } /** * Remove all delivered notifications. */ removeAllDeliveredNotifications() { return (0, _native.getNativeModule)(this).removeAllDeliveredNotifications(); } /** * Remove a delivered notification. * @param notificationId */ removeDeliveredNotification(notificationId) { if (!notificationId) { return Promise.reject(new Error('Notifications: removeDeliveredNotification expects a `notificationId`')); } return (0, _native.getNativeModule)(this).removeDeliveredNotification(notificationId); } /** * Schedule a notification * @param notification * @returns {*} */ scheduleNotification(notification, schedule) { if (!(notification instanceof _Notification.default)) { return Promise.reject(new Error(`Notifications:scheduleNotification expects a 'Notification' but got type ${typeof notification}`)); } try { const nativeNotification = notification.build(); nativeNotification.schedule = schedule; return (0, _native.getNativeModule)(this).scheduleNotification(nativeNotification); } catch (error) { return Promise.reject(error); } } setBadge(badge) { return (0, _native.getNativeModule)(this).setBadge(badge); } } exports.default = Notifications; const statics = { Android: { Action: _AndroidAction.default, BadgeIconType: _types.BadgeIconType, Category: _types.Category, Channel: _AndroidChannel.default, ChannelGroup: _AndroidChannelGroup.default, Defaults: _types.Defaults, GroupAlert: _types.GroupAlert, Importance: _types.Importance, Priority: _types.Priority, RemoteInput: _AndroidRemoteInput.default, SemanticAction: _types.SemanticAction, Visibility: _types.Visibility }, Notification: _Notification.default }; exports.statics = statics;