[fcm] Improved RemoteMessage; Exported constants as part of messaging statics;

This commit is contained in:
Chris Bianca 2018-02-09 07:50:51 +00:00
parent e2f56ac93c
commit 326d1778c6
2 changed files with 91 additions and 71 deletions

View File

@ -1,61 +1,39 @@
/** /**
* @flow * @flow
* Remote message representation wrapper * RemoteMessage representation wrapper
*/ */
import { isObject, generatePushID } from './../../utils'; import { isObject, generatePushID } from './../../utils';
type NativeRemoteMessage = {
collapseKey?: string,
data: { [string]: string },
messageId: string,
messageType?: string,
to: string,
ttl: number,
};
export default class RemoteMessage { export default class RemoteMessage {
properties: Object; collapseKey: string | void;
data: { [string]: string };
messageId: string;
messageType: string | void;
to: string;
ttl: number;
constructor(sender: string) { constructor() {
this.properties = { this.data = {};
id: generatePushID(), this.messageId = generatePushID();
ttl: 3600, this.ttl = 3600;
// add the googleapis sender id part if not already added.
sender: `${sender}`.includes('@')
? sender
: `${sender}@gcm.googleapis.com`,
type: 'remote',
data: {},
};
} }
/** /**
* *
* @param ttl * @param collapseKey
* @returns {RemoteMessage} * @returns {RemoteMessage}
*/ */
setTtl(ttl: number): RemoteMessage { withCollapseKey(collapseKey: string): RemoteMessage {
this.properties.ttl = ttl; this.collapseKey = collapseKey;
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}`;
return this; return this;
} }
@ -64,26 +42,64 @@ export default class RemoteMessage {
* @param data * @param data
* @returns {RemoteMessage} * @returns {RemoteMessage}
*/ */
setData(data: Object = {}) { withData(data: Object = {}) {
if (!isObject(data)) { if (!isObject(data)) {
throw new Error( 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}'.`
); );
} }
this.data = 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]}`;
}
return this; 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,
};
} }
} }

View File

@ -9,6 +9,12 @@ import ModuleBase from '../../utils/ModuleBase';
import { getNativeModule } from '../../utils/native'; import { getNativeModule } from '../../utils/native';
import { isFunction, isObject } from '../../utils'; import { isFunction, isObject } from '../../utils';
import Message from './Message'; import Message from './Message';
import RemoteMessage from './RemoteMessage';
import {
MessageType,
RemoteNotificationResult,
WillPresentNotificationResult,
} from './types';
import type App from '../core/firebase-app'; import type App from '../core/firebase-app';
import type { NativeMessage } from './types'; import type { NativeMessage } from './types';
@ -25,15 +31,6 @@ type OnTokenRefreshObserver = {
next: OnTokenRefresh, next: OnTokenRefresh,
}; };
type RemoteMessage = {
collapseKey?: string,
data: { [string]: string },
messageId: string,
messageType?: string,
to: string,
ttl: number,
};
const NATIVE_EVENTS = [ const NATIVE_EVENTS = [
'messaging_message_received', 'messaging_message_received',
'messaging_token_refreshed', 'messaging_token_refreshed',
@ -90,7 +87,6 @@ export default class Messaging extends ModuleBase {
); );
} }
// TODO: iOS finish
getLogger(this).info('Creating onMessage listener'); getLogger(this).info('Creating onMessage listener');
const wrappedListener = async (nativeMessage: NativeMessage) => { const wrappedListener = async (nativeMessage: NativeMessage) => {
@ -151,7 +147,12 @@ export default class Messaging extends ModuleBase {
} }
sendMessage(remoteMessage: RemoteMessage): Promise<void> { sendMessage(remoteMessage: RemoteMessage): Promise<void> {
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 { setBadge(badge: number): void {
@ -199,5 +200,8 @@ export default class Messaging extends ModuleBase {
} }
export const statics = { export const statics = {
// RemoteMessage, MessageType,
RemoteMessage,
RemoteNotificationResult,
WillPresentNotificationResult,
}; };