react-native-firebase/lib/modules/messaging/index.js

290 lines
7.5 KiB
JavaScript
Raw Normal View History

/**
* @flow
* Messaging representation wrapper
*/
2017-10-03 08:49:46 +00:00
import { Platform, NativeModules } from 'react-native';
import { SharedEventEmitter } from '../../utils/events';
import ModuleBase from '../../utils/ModuleBase';
import RemoteMessage from './RemoteMessage';
import { getNativeModule } from '../../utils/native';
2017-03-02 12:58:15 +00:00
import type App from '../core/firebase-app';
const EVENT_TYPE = {
RefreshToken: 'messaging_token_refreshed',
Notification: 'messaging_notification_received',
};
const NOTIFICATION_TYPE = {
Remote: 'remote_notification',
NotificationResponse: 'notification_response',
WillPresent: 'will_present_notification',
Local: 'local_notification',
};
const REMOTE_NOTIFICATION_RESULT = {
NewData: 'UIBackgroundFetchResultNewData',
NoData: 'UIBackgroundFetchResultNoData',
ResultFailed: 'UIBackgroundFetchResultFailed',
};
const WILL_PRESENT_RESULT = {
All: 'UNNotificationPresentationOptionAll',
None: 'UNNotificationPresentationOptionNone',
};
2017-03-02 12:58:15 +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
/**
* 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;
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);
break;
default:
}
}
}
export const MODULE_NAME = 'RNFirebaseMessaging';
export const NAMESPACE = 'messaging';
2017-03-02 12:58:15 +00:00
/**
* @class Messaging
*/
export default class Messaging extends ModuleBase {
constructor(app: App) {
super(app, {
events: NATIVE_EVENTS,
moduleName: MODULE_NAME,
multiApp: false,
namespace: NAMESPACE,
});
2017-03-02 12:58:15 +00:00
}
get EVENT_TYPE(): Object {
return EVENT_TYPE;
}
get NOTIFICATION_TYPE(): Object {
return NOTIFICATION_TYPE;
}
get REMOTE_NOTIFICATION_RESULT(): Object {
return REMOTE_NOTIFICATION_RESULT;
2017-03-02 12:58:15 +00:00
}
get WILL_PRESENT_RESULT(): Object {
return WILL_PRESENT_RESULT;
2017-03-02 12:58:15 +00:00
}
/**
* Returns the notification that triggered application open
* @returns {*}
*/
getInitialNotification(): Promise<Object> {
return getNativeModule(this).getInitialNotification();
2017-03-02 12:58:15 +00:00
}
/**
* Returns the fcm token for the current device
* @returns {*|Promise.<String>}
*/
getToken(): Promise<string> {
return getNativeModule(this).getToken();
2017-03-02 12:58:15 +00:00
}
/**
* Reset Instance ID and revokes all tokens.
* @returns {*|Promise.<*>}
*/
deleteInstanceId(): Promise<void> {
return getNativeModule(this).deleteInstanceId();
}
/**
* Create and display a local notification
* @param notification
* @returns {*}
*/
createLocalNotification(notification: Object): Promise<void> {
const _notification = Object.assign({}, notification);
_notification.id = _notification.id || new Date().getTime().toString();
_notification.local_notification = true;
return getNativeModule(this).createLocalNotification(_notification);
2017-03-02 12:58:15 +00:00
}
/**
*
* @param notification
* @returns {*}
*/
scheduleLocalNotification(notification: Object): Promise<void> {
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;
return getNativeModule(this).scheduleLocalNotification(_notification);
2017-03-02 12:58:15 +00:00
}
/**
* Returns an array of all scheduled notifications
* @returns {Promise.<Array>}
*/
getScheduledLocalNotifications(): Promise<Object[]> {
return getNativeModule(this).getScheduledLocalNotifications();
2017-03-02 12:58:15 +00:00
}
/**
* Cancel a local notification by id - using '*' will cancel
* all local notifications.
* @param id
* @returns {*}
*/
cancelLocalNotification(id: string): Promise<void> {
if (!id) return Promise.reject(new Error('Missing notification id'));
if (id === '*') return getNativeModule(this).cancelAllLocalNotifications();
return getNativeModule(this).cancelLocalNotification(id);
2017-03-02 12:58:15 +00:00
}
/**
* Remove a delivered notification - using '*' will remove
* all delivered notifications.
* @param id
* @returns {*}
*/
removeDeliveredNotification(id: string): Promise<void> {
if (!id) return Promise.reject(new Error('Missing notification id'));
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.<*>}
*/
requestPermissions(): Promise<void> {
return getNativeModule(this).requestPermissions();
2017-03-21 11:26:16 +00:00
}
/**
* Set notification count badge number
2017-03-21 11:26:16 +00:00
* @param n
*/
setBadgeNumber(n: number): void {
getNativeModule(this).setBadgeNumber(n);
2017-03-02 12:58:15 +00:00
}
/**
* set notification count badge number
* @returns {Promise.<Number>}
*/
getBadgeNumber(): Promise<number> {
return getNativeModule(this).getBadgeNumber();
2017-03-02 12:58:15 +00:00
}
/**
* Subscribe to messages / notifications
* @param listener
* @returns {*}
*/
onMessage(listener: (Object) => any): () => any {
const rnListener = SharedEventEmitter.addListener(
EVENT_TYPE.Notification,
async (event) => {
const data = {
...event,
finish,
};
await listener(data);
if (!data._finishCalled) {
data.finish();
}
},
);
return () => rnListener.remove();
}
/**
* Subscribe to token refresh events
* @param listener
* @returns {*}
*/
onTokenRefresh(listener: (string) => any): () => any {
const rnListener = SharedEventEmitter.addListener(EVENT_TYPE.RefreshToken, listener);
return () => rnListener.remove();
2017-03-02 12:58:15 +00:00
}
/**
* Subscribe to a topic
* @param topic
*/
subscribeToTopic(topic: string): void {
getNativeModule(this).subscribeToTopic(topic);
}
/**
* Unsubscribe from a topic
* @param topic
*/
unsubscribeFromTopic(topic: string): void {
getNativeModule(this).unsubscribeFromTopic(topic);
2017-03-02 12:58:15 +00:00
}
/**
* Send an upstream message
* @param remoteMessage
*/
send(remoteMessage: RemoteMessage): Promise<void> {
if (!(remoteMessage instanceof RemoteMessage)) {
throw new Error('messaging().send requires an instance of RemoteMessage as the first argument.');
}
return getNativeModule(this).send(remoteMessage.toJSON());
}
2017-03-02 12:58:15 +00:00
}
export const statics = {
EVENT_TYPE,
NOTIFICATION_TYPE,
REMOTE_NOTIFICATION_RESULT,
WILL_PRESENT_RESULT,
RemoteMessage,
};