diff --git a/lib/modules/messaging/RemoteMessage.js b/lib/modules/messaging/RemoteMessage.js index 41370ef8..de41d8b7 100644 --- a/lib/modules/messaging/RemoteMessage.js +++ b/lib/modules/messaging/RemoteMessage.js @@ -1,61 +1,39 @@ /** * @flow - * Remote message representation wrapper + * RemoteMessage representation wrapper */ import { isObject, generatePushID } from './../../utils'; +type NativeRemoteMessage = { + collapseKey?: string, + data: { [string]: string }, + messageId: string, + messageType?: string, + to: string, + ttl: number, +}; + export default class RemoteMessage { - properties: Object; + collapseKey: string | void; + data: { [string]: string }; + messageId: string; + messageType: string | void; + to: string; + ttl: number; - constructor(sender: string) { - this.properties = { - id: generatePushID(), - ttl: 3600, - // add the googleapis sender id part if not already added. - sender: `${sender}`.includes('@') - ? sender - : `${sender}@gcm.googleapis.com`, - type: 'remote', - data: {}, - }; + constructor() { + this.data = {}; + this.messageId = generatePushID(); + this.ttl = 3600; } /** * - * @param ttl + * @param collapseKey * @returns {RemoteMessage} */ - setTtl(ttl: number): RemoteMessage { - this.properties.ttl = ttl; - return this; - } - - /** - * - * @param id - */ - setId(id: string): RemoteMessage { - this.properties.id = `${id}`; - return this; - } - - /** - * - * @param type - * @returns {RemoteMessage} - */ - setType(type: string): RemoteMessage { - this.properties.type = `${type}`; - return this; - } - - /** - * - * @param key - * @returns {RemoteMessage} - */ - setCollapseKey(key: string): RemoteMessage { - this.properties.collapseKey = `${key}`; + withCollapseKey(collapseKey: string): RemoteMessage { + this.collapseKey = collapseKey; return this; } @@ -64,26 +42,64 @@ export default class RemoteMessage { * @param data * @returns {RemoteMessage} */ - setData(data: Object = {}) { + withData(data: Object = {}) { if (!isObject(data)) { throw new Error( - `RemoteMessage:setData expects an object as the first parameter but got type '${typeof data}'.` + `RemoteMessage:withData expects an object but got type '${typeof data}'.` ); } - - const props = Object.keys(data); - - // coerce all property values to strings as - // remote message data only supports strings - for (let i = 0, len = props.length; i < len; i++) { - const prop = props[i]; - this.properties.data[prop] = `${data[prop]}`; - } - + this.data = data; return this; } - toJSON(): Object { - return Object.assign({}, this.properties); + /** + * + * @param messageId + * @returns {RemoteMessage} + */ + withMessageId(messageId: string): RemoteMessage { + this.messageId = messageId; + return this; + } + + /** + * + * @param messageType + * @returns {RemoteMessage} + */ + withMessageType(messageType: string): RemoteMessage { + this.messageType = messageType; + return this; + } + + /** + * + * @param ttl + * @returns {RemoteMessage} + */ + withTtl(ttl: number): RemoteMessage { + this.ttl = ttl; + return this; + } + + build(): NativeRemoteMessage { + if (!this.data) { + throw new Error('RemoteMessage: Missing required `data` property'); + } else if (!this.messageId) { + throw new Error('RemoteMessage: Missing required `messageId` property'); + } else if (!this.to) { + throw new Error('RemoteMessage: Missing required `to` property'); + } else if (!this.ttl) { + throw new Error('RemoteMessage: Missing required `ttl` property'); + } + + return { + collapseKey: this.collapseKey, + data: this.data, + messageId: this.messageId, + messageType: this.messageType, + to: this.to, + ttl: this.ttl, + }; } } diff --git a/lib/modules/messaging/index.js b/lib/modules/messaging/index.js index d411afc4..ed843f99 100644 --- a/lib/modules/messaging/index.js +++ b/lib/modules/messaging/index.js @@ -9,6 +9,12 @@ import ModuleBase from '../../utils/ModuleBase'; import { getNativeModule } from '../../utils/native'; import { isFunction, isObject } from '../../utils'; import Message from './Message'; +import RemoteMessage from './RemoteMessage'; +import { + MessageType, + RemoteNotificationResult, + WillPresentNotificationResult, +} from './types'; import type App from '../core/firebase-app'; import type { NativeMessage } from './types'; @@ -25,15 +31,6 @@ type OnTokenRefreshObserver = { next: OnTokenRefresh, }; -type RemoteMessage = { - collapseKey?: string, - data: { [string]: string }, - messageId: string, - messageType?: string, - to: string, - ttl: number, -}; - const NATIVE_EVENTS = [ 'messaging_message_received', 'messaging_token_refreshed', @@ -90,7 +87,6 @@ export default class Messaging extends ModuleBase { ); } - // TODO: iOS finish getLogger(this).info('Creating onMessage listener'); const wrappedListener = async (nativeMessage: NativeMessage) => { @@ -151,7 +147,12 @@ export default class Messaging extends ModuleBase { } sendMessage(remoteMessage: RemoteMessage): Promise { - return getNativeModule(this).send(remoteMessage); + if (!(remoteMessage instanceof RemoteMessage)) { + throw new Error( + `Messaging:sendMessage expects a 'RemoteMessage' but got type ${typeof remoteMessage}` + ); + } + return getNativeModule(this).send(remoteMessage.build()); } setBadge(badge: number): void { @@ -199,5 +200,8 @@ export default class Messaging extends ModuleBase { } export const statics = { - // RemoteMessage, + MessageType, + RemoteMessage, + RemoteNotificationResult, + WillPresentNotificationResult, };