2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
2018-02-02 08:40:48 +00:00
|
|
|
* Messaging (FCM) representation wrapper
|
2017-12-04 12:07:41 +00:00
|
|
|
*/
|
2018-04-13 13:22:17 +00:00
|
|
|
import { Platform } from 'react-native';
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
2018-02-02 08:40:48 +00:00
|
|
|
import INTERNALS from '../../utils/internals';
|
|
|
|
import { getLogger } from '../../utils/log';
|
2017-12-22 15:24:31 +00:00
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { getNativeModule } from '../../utils/native';
|
2018-02-02 08:40:48 +00:00
|
|
|
import { isFunction, isObject } from '../../utils';
|
2018-02-09 07:50:51 +00:00
|
|
|
import RemoteMessage from './RemoteMessage';
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-14 13:00:19 +00:00
|
|
|
import type App from '../core/app';
|
2018-02-23 16:11:59 +00:00
|
|
|
import type { NativeInboundRemoteMessage } from './types';
|
2017-03-17 16:04:39 +00:00
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
type OnMessage = RemoteMessage => any;
|
2017-03-17 16:04:39 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
type OnMessageObserver = {
|
|
|
|
next: OnMessage,
|
2017-03-17 16:04:39 +00:00
|
|
|
};
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-04-19 15:01:40 +00:00
|
|
|
type OnTokenRefresh = string => any;
|
2017-12-22 15:57:33 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
type OnTokenRefreshObserver = {
|
|
|
|
next: OnTokenRefresh,
|
|
|
|
};
|
2017-10-03 08:49:46 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
const NATIVE_EVENTS = [
|
|
|
|
'messaging_message_received',
|
|
|
|
'messaging_token_refreshed',
|
|
|
|
];
|
2017-03-17 16:04:39 +00:00
|
|
|
|
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,
|
2017-11-21 23:37:05 +00:00
|
|
|
hasShards: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onMessage
|
|
|
|
'messaging_message_received',
|
2018-02-23 16:11:59 +00:00
|
|
|
(message: NativeInboundRemoteMessage) => {
|
|
|
|
SharedEventEmitter.emit('onMessage', new RemoteMessage(message));
|
2018-02-02 08:40:48 +00:00
|
|
|
}
|
|
|
|
);
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
SharedEventEmitter.addListener(
|
|
|
|
// sub to internal native event - this fans out to
|
|
|
|
// public event name: onMessage
|
|
|
|
'messaging_token_refreshed',
|
|
|
|
(token: string) => {
|
|
|
|
SharedEventEmitter.emit('onTokenRefresh', token);
|
|
|
|
}
|
|
|
|
);
|
2018-04-13 13:22:17 +00:00
|
|
|
|
|
|
|
// Tell the native module that we're ready to receive events
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
getNativeModule(this).jsInitialised();
|
|
|
|
}
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
onMessage(nextOrObserver: OnMessage | OnMessageObserver): () => any {
|
2018-02-23 16:11:59 +00:00
|
|
|
let listener: RemoteMessage => any;
|
2018-02-02 08:40:48 +00:00
|
|
|
if (isFunction(nextOrObserver)) {
|
2018-03-08 10:42:35 +00:00
|
|
|
// $FlowExpectedError: Not coping with the overloaded method signature
|
2018-02-02 08:40:48 +00:00
|
|
|
listener = nextOrObserver;
|
|
|
|
} else if (isObject(nextOrObserver) && isFunction(nextOrObserver.next)) {
|
|
|
|
listener = nextOrObserver.next;
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
'Messaging.onMessage failed: First argument must be a function or observer object with a `next` function.'
|
2018-01-25 18:25:39 +00:00
|
|
|
);
|
2018-02-02 08:40:48 +00:00
|
|
|
}
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
getLogger(this).info('Creating onMessage listener');
|
2018-02-08 17:07:20 +00:00
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
SharedEventEmitter.addListener('onMessage', listener);
|
2018-02-02 08:40:48 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
getLogger(this).info('Removing onMessage listener');
|
2018-02-23 16:11:59 +00:00
|
|
|
SharedEventEmitter.removeListener('onMessage', listener);
|
2018-02-02 08:40:48 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onTokenRefresh(
|
|
|
|
nextOrObserver: OnTokenRefresh | OnTokenRefreshObserver
|
|
|
|
): () => any {
|
2018-04-19 15:01:40 +00:00
|
|
|
let listener: string => any;
|
2018-02-02 08:40:48 +00:00
|
|
|
if (isFunction(nextOrObserver)) {
|
2018-03-08 10:42:35 +00:00
|
|
|
// $FlowExpectedError: Not coping with the overloaded method signature
|
2018-02-02 08:40:48 +00:00
|
|
|
listener = nextOrObserver;
|
|
|
|
} else if (isObject(nextOrObserver) && isFunction(nextOrObserver.next)) {
|
|
|
|
listener = nextOrObserver.next;
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
'Messaging.OnTokenRefresh failed: First argument must be a function or observer object with a `next` function.'
|
|
|
|
);
|
|
|
|
}
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
getLogger(this).info('Creating onTokenRefresh listener');
|
|
|
|
SharedEventEmitter.addListener('onTokenRefresh', listener);
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
return () => {
|
|
|
|
getLogger(this).info('Removing onTokenRefresh listener');
|
|
|
|
SharedEventEmitter.removeListener('onTokenRefresh', listener);
|
|
|
|
};
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
requestPermission(): Promise<void> {
|
|
|
|
return getNativeModule(this).requestPermission();
|
2017-03-21 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:04:39 +00:00
|
|
|
/**
|
2018-02-02 08:40:48 +00:00
|
|
|
* NON WEB-SDK METHODS
|
2017-03-17 16:04:39 +00:00
|
|
|
*/
|
2018-02-05 18:04:10 +00:00
|
|
|
hasPermission(): Promise<boolean> {
|
|
|
|
return getNativeModule(this).hasPermission();
|
|
|
|
}
|
2017-03-02 12:58:15 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
sendMessage(remoteMessage: RemoteMessage): Promise<void> {
|
2018-02-09 07:50:51 +00:00
|
|
|
if (!(remoteMessage instanceof RemoteMessage)) {
|
2018-03-23 17:03:53 +00:00
|
|
|
return Promise.reject(
|
|
|
|
new Error(
|
|
|
|
`Messaging:sendMessage expects a 'RemoteMessage' but got type ${typeof remoteMessage}`
|
|
|
|
)
|
2018-02-09 07:50:51 +00:00
|
|
|
);
|
|
|
|
}
|
2018-03-23 17:03:53 +00:00
|
|
|
try {
|
|
|
|
return getNativeModule(this).sendMessage(remoteMessage.build());
|
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
2017-03-17 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:02:39 +00:00
|
|
|
subscribeToTopic(topic: string): Promise<void> {
|
|
|
|
return getNativeModule(this).subscribeToTopic(topic);
|
2017-03-17 16:04:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 10:02:39 +00:00
|
|
|
unsubscribeFromTopic(topic: string): Promise<void> {
|
|
|
|
return getNativeModule(this).unsubscribeFromTopic(topic);
|
2017-03-02 12:58:15 +00:00
|
|
|
}
|
2017-03-21 15:11:07 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-02 08:40:48 +00:00
|
|
|
* KNOWN UNSUPPORTED METHODS
|
2017-03-21 15:11:07 +00:00
|
|
|
*/
|
2017-03-30 15:25:27 +00:00
|
|
|
|
2018-02-02 08:40:48 +00:00
|
|
|
deleteToken() {
|
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
|
|
|
'messaging',
|
|
|
|
'deleteToken'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
setBackgroundMessageHandler() {
|
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
|
|
|
'messaging',
|
|
|
|
'setBackgroundMessageHandler'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
useServiceWorker() {
|
|
|
|
throw new Error(
|
|
|
|
INTERNALS.STRINGS.ERROR_UNSUPPORTED_MODULE_METHOD(
|
|
|
|
'messaging',
|
|
|
|
'useServiceWorker'
|
|
|
|
)
|
|
|
|
);
|
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 = {
|
2018-02-09 07:50:51 +00:00
|
|
|
RemoteMessage,
|
2017-03-30 15:25:27 +00:00
|
|
|
};
|