2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Messaging representation wrapper
|
|
|
|
*/
|
2017-10-03 08:49:46 +00:00
|
|
|
import { Platform, NativeModules } from 'react-native';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2017-03-30 15:25:27 +00:00
|
|
|
import RemoteMessage from './RemoteMessage';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
import type App from '../core/firebase-app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
const EVENT_TYPE = {
|
2017-05-30 07:26:40 +00:00
|
|
|
RefreshToken: 'messaging_token_refreshed',
|
|
|
|
Notification: 'messaging_notification_received',
|
2017-03-17 16:04:39 +00:00
|
|
|
};
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
const NOTIFICATION_TYPE = {
|
2017-03-17 16:04:39 +00:00
|
|
|
Remote: 'remote_notification',
|
|
|
|
NotificationResponse: 'notification_response',
|
|
|
|
WillPresent: 'will_present_notification',
|
|
|
|
Local: 'local_notification',
|
|
|
|
};
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
const REMOTE_NOTIFICATION_RESULT = {
|
2017-03-17 16:04:39 +00:00
|
|
|
NewData: 'UIBackgroundFetchResultNewData',
|
|
|
|
NoData: 'UIBackgroundFetchResultNoData',
|
|
|
|
ResultFailed: 'UIBackgroundFetchResultFailed',
|
|
|
|
};
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
const WILL_PRESENT_RESULT = {
|
2017-03-17 16:04:39 +00:00
|
|
|
All: 'UNNotificationPresentationOptionAll',
|
|
|
|
None: 'UNNotificationPresentationOptionNone',
|
|
|
|
};
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2017-12-22 15:57:33 +00:00
|
|
|
const NATIVE_EVENTS = [
|
|
|
|
EVENT_TYPE.RefreshToken,
|
|
|
|
EVENT_TYPE.Notification,
|
|
|
|
];
|
|
|
|
|
2017-10-06 13:41:13 +00:00
|
|
|
const FirebaseMessaging = NativeModules.RNFirebaseMessaging;
|
2017-10-03 08:49:46 +00:00
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* IOS only finish function
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
function finish(data) {
|
|
|
|
if (Platform.OS !== 'ios') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!this._finishCalled && this._completionHandlerId) {
|
2017-03-27 12:26:26 +00:00
|
|
|
let result = data;
|
2017-03-17 16:04:39 +00:00
|
|
|
|
|
|
|
this._finishCalled = true;
|
|
|
|
|
|
|
|
switch (this._notificationType) {
|
|
|
|
case NOTIFICATION_TYPE.Remote:
|
|
|
|
result = result || REMOTE_NOTIFICATION_RESULT.NoData;
|
|
|
|
if (!Object.values(REMOTE_NOTIFICATION_RESULT).includes(result)) {
|
|
|
|
throw new Error('Invalid REMOTE_NOTIFICATION_RESULT value, use messaging().REMOTE_NOTIFICATION_RESULT');
|
|
|
|
}
|
|
|
|
|
|
|
|
FirebaseMessaging.finishRemoteNotification(this._completionHandlerId, result);
|
|
|
|
return;
|
|
|
|
case NOTIFICATION_TYPE.NotificationResponse:
|
|
|
|
FirebaseMessaging.finishNotificationResponse(this._completionHandlerId);
|
|
|
|
return;
|
|
|
|
case NOTIFICATION_TYPE.WillPresent:
|
|
|
|
result = result || (this.show_in_foreground ? WILL_PRESENT_RESULT.All : WILL_PRESENT_RESULT.None);
|
|
|
|
if (!Object.values(WILL_PRESENT_RESULT).includes(result)) {
|
|
|
|
throw new Error('Invalid WILL_PRESENT_RESULT value, use messaging().WILL_PRESENT_RESULT');
|
|
|
|
}
|
|
|
|
|
|
|
|
FirebaseMessaging.finishWillPresentNotification(this._completionHandlerId, result);
|
2017-06-30 16:23:32 +00:00
|
|
|
break;
|
2017-03-17 16:04:39 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseMessaging';
|
|
|
|
export const NAMESPACE = 'messaging';
|
2017-03-17 16:04:39 +00:00
|
|
|
|
2017-03-02 12:58:15 +00:00
|
|
|
/**
|
|
|
|
* @class Messaging
|
|
|
|
*/
|
2017-06-30 16:23:32 +00:00
|
|
|
export default class Messaging extends ModuleBase {
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
get EVENT_TYPE(): Object {
|
2017-03-17 16:04:39 +00:00
|
|
|
return EVENT_TYPE;
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
get NOTIFICATION_TYPE(): Object {
|
2017-03-17 16:04:39 +00:00
|
|
|
return NOTIFICATION_TYPE;
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
get REMOTE_NOTIFICATION_RESULT(): Object {
|
2017-03-17 16:04:39 +00:00
|
|
|
return REMOTE_NOTIFICATION_RESULT;
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
get WILL_PRESENT_RESULT(): Object {
|
2017-03-17 16:04:39 +00:00
|
|
|
return WILL_PRESENT_RESULT;
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the notification that triggered application open
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getInitialNotification(): Promise<Object> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getInitialNotification();
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the fcm token for the current device
|
|
|
|
* @returns {*|Promise.<String>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getToken(): Promise<string> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getToken();
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 14:33:20 +00:00
|
|
|
/**
|
|
|
|
* Reset Instance ID and revokes all tokens.
|
|
|
|
* @returns {*|Promise.<*>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
deleteInstanceId(): Promise<void> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).deleteInstanceId();
|
2017-09-26 14:33:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Create and display a local notification
|
|
|
|
* @param notification
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
createLocalNotification(notification: Object): Promise<void> {
|
2017-03-17 16:04:39 +00:00
|
|
|
const _notification = Object.assign({}, notification);
|
|
|
|
_notification.id = _notification.id || new Date().getTime().toString();
|
|
|
|
_notification.local_notification = true;
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).createLocalNotification(_notification);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param notification
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
scheduleLocalNotification(notification: Object): Promise<void> {
|
2017-03-17 16:04:39 +00:00
|
|
|
const _notification = Object.assign({}, notification);
|
|
|
|
if (!notification.id) return Promise.reject(new Error('An id is required to schedule a local notification.'));
|
|
|
|
_notification.local_notification = true;
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).scheduleLocalNotification(_notification);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Returns an array of all scheduled notifications
|
|
|
|
* @returns {Promise.<Array>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getScheduledLocalNotifications(): Promise<Object[]> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getScheduledLocalNotifications();
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Cancel a local notification by id - using '*' will cancel
|
|
|
|
* all local notifications.
|
|
|
|
* @param id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
cancelLocalNotification(id: string): Promise<void> {
|
|
|
|
if (!id) return Promise.reject(new Error('Missing notification id'));
|
2018-01-05 17:20:02 +00:00
|
|
|
if (id === '*') return getNativeModule(this).cancelAllLocalNotifications();
|
|
|
|
return getNativeModule(this).cancelLocalNotification(id);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Remove a delivered notification - using '*' will remove
|
|
|
|
* all delivered notifications.
|
|
|
|
* @param id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
removeDeliveredNotification(id: string): Promise<void> {
|
|
|
|
if (!id) return Promise.reject(new Error('Missing notification id'));
|
2018-01-05 17:20:02 +00:00
|
|
|
if (id === '*') return getNativeModule(this).removeAllDeliveredNotifications();
|
|
|
|
return getNativeModule(this).removeDeliveredNotification(id);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 11:26:16 +00:00
|
|
|
/**
|
|
|
|
* Request notification permission
|
|
|
|
* @platforms ios
|
|
|
|
* @returns {*|Promise.<*>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
requestPermissions(): Promise<void> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).requestPermissions();
|
2017-03-21 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Set notification count badge number
|
2017-03-21 11:26:16 +00:00
|
|
|
* @param n
|
2017-03-17 16:04:39 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
setBadgeNumber(n: number): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).setBadgeNumber(n);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* set notification count badge number
|
|
|
|
* @returns {Promise.<Number>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getBadgeNumber(): Promise<number> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getBadgeNumber();
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Subscribe to messages / notifications
|
|
|
|
* @param listener
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
onMessage(listener: (Object) => any): () => any {
|
2017-12-22 15:24:31 +00:00
|
|
|
const rnListener = SharedEventEmitter.addListener(
|
2017-05-30 07:26:40 +00:00
|
|
|
EVENT_TYPE.Notification,
|
2017-06-30 16:23:32 +00:00
|
|
|
async (event) => {
|
2017-05-30 07:26:40 +00:00
|
|
|
const data = {
|
|
|
|
...event,
|
|
|
|
finish,
|
|
|
|
};
|
|
|
|
await listener(data);
|
|
|
|
|
|
|
|
if (!data._finishCalled) {
|
|
|
|
data.finish();
|
|
|
|
}
|
2017-06-30 16:23:32 +00:00
|
|
|
},
|
2017-06-19 12:14:36 +00:00
|
|
|
);
|
|
|
|
return () => rnListener.remove();
|
2017-03-17 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to token refresh events
|
|
|
|
* @param listener
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
onTokenRefresh(listener: (string) => any): () => any {
|
2017-12-22 15:24:31 +00:00
|
|
|
const rnListener = SharedEventEmitter.addListener(EVENT_TYPE.RefreshToken, listener);
|
2017-06-19 12:14:36 +00:00
|
|
|
return () => rnListener.remove();
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
|
|
|
* Subscribe to a topic
|
|
|
|
* @param topic
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
subscribeToTopic(topic: string): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).subscribeToTopic(topic);
|
2017-03-17 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribe from a topic
|
|
|
|
* @param topic
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
unsubscribeFromTopic(topic: string): void {
|
2018-01-05 17:20:02 +00:00
|
|
|
getNativeModule(this).unsubscribeFromTopic(topic);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
2017-03-21 15:11:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an upstream message
|
2017-03-30 15:25:27 +00:00
|
|
|
* @param remoteMessage
|
2017-03-21 15:11:07 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
send(remoteMessage: RemoteMessage): Promise<void> {
|
2017-03-30 15:25:27 +00:00
|
|
|
if (!(remoteMessage instanceof RemoteMessage)) {
|
|
|
|
throw new Error('messaging().send requires an instance of RemoteMessage as the first argument.');
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).send(remoteMessage.toJSON());
|
2017-03-21 15:11:07 +00:00
|
|
|
}
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
2017-03-30 15:25:27 +00:00
|
|
|
|
|
|
|
export const statics = {
|
|
|
|
EVENT_TYPE,
|
|
|
|
NOTIFICATION_TYPE,
|
|
|
|
REMOTE_NOTIFICATION_RESULT,
|
|
|
|
WILL_PRESENT_RESULT,
|
|
|
|
RemoteMessage,
|
|
|
|
};
|