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

156 lines
3.2 KiB
JavaScript
Raw Normal View History

/**
* @flow
* RemoteMessage representation wrapper
*/
import { isObject, generatePushID } from './../../utils';
2018-02-23 16:11:59 +00:00
import type {
NativeInboundRemoteMessage,
NativeOutboundRemoteMessage,
} from './types';
export default class RemoteMessage {
2018-02-09 16:59:29 +00:00
_collapseKey: string | void;
_data: { [string]: string };
2018-02-23 16:11:59 +00:00
_from: string | void;
2018-02-09 16:59:29 +00:00
_messageId: string;
_messageType: string | void;
2018-02-23 16:11:59 +00:00
_sentTime: number | void;
2018-02-09 16:59:29 +00:00
_to: string;
_ttl: number;
2018-02-23 16:11:59 +00:00
constructor(inboundMessage?: NativeInboundRemoteMessage) {
if (inboundMessage) {
this._collapseKey = inboundMessage.collapseKey;
this._data = inboundMessage.data;
this._from = inboundMessage.from;
this._messageId = inboundMessage.messageId;
this._messageType = inboundMessage.messageType;
2018-02-23 16:11:59 +00:00
this._sentTime = inboundMessage.sentTime;
}
// defaults
this._data = this._data || {};
2018-02-09 16:59:29 +00:00
// TODO: Is this the best way to generate an ID?
2018-02-23 16:11:59 +00:00
this._messageId = this._messageId || generatePushID();
2018-02-09 16:59:29 +00:00
this._ttl = 3600;
}
2018-02-23 16:11:59 +00:00
get collapseKey(): ?string {
return this._collapseKey;
}
get data(): { [string]: string } {
return this._data;
}
get from(): ?string {
return this._from;
}
get messageId(): ?string {
return this._messageId;
}
get messageType(): ?string {
return this._messageType;
}
2018-02-23 16:11:59 +00:00
get sentTime(): ?number {
return this._sentTime;
}
get to(): ?string {
return this._to;
}
get ttl(): ?number {
return this._ttl;
}
/**
*
* @param collapseKey
* @returns {RemoteMessage}
*/
2018-02-09 16:59:29 +00:00
setCollapseKey(collapseKey: string): RemoteMessage {
this._collapseKey = collapseKey;
return this;
}
/**
*
* @param data
* @returns {RemoteMessage}
*/
2018-02-09 16:59:29 +00:00
setData(data: { [string]: string } = {}) {
if (!isObject(data)) {
throw new Error(
2018-02-09 16:59:29 +00:00
`RemoteMessage:setData expects an object but got type '${typeof data}'.`
);
}
2018-02-09 16:59:29 +00:00
this._data = data;
return this;
}
/**
*
* @param messageId
* @returns {RemoteMessage}
*/
2018-02-09 16:59:29 +00:00
setMessageId(messageId: string): RemoteMessage {
this._messageId = messageId;
return this;
}
/**
*
* @param messageType
* @returns {RemoteMessage}
*/
2018-02-09 16:59:29 +00:00
setMessageType(messageType: string): RemoteMessage {
this._messageType = messageType;
return this;
}
2018-02-23 16:36:59 +00:00
/**
*
* @param to
* @returns {RemoteMessage}
*/
setTo(to: string): RemoteMessage {
this._to = to;
return this;
}
/**
*
* @param ttl
* @returns {RemoteMessage}
*/
2018-02-09 16:59:29 +00:00
setTtl(ttl: number): RemoteMessage {
this._ttl = ttl;
return this;
}
2018-02-23 16:11:59 +00:00
build(): NativeOutboundRemoteMessage {
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 {
2018-02-09 16:59:29 +00:00
collapseKey: this._collapseKey,
data: this._data,
messageId: this._messageId,
messageType: this._messageType,
to: this._to,
ttl: this._ttl,
};
}
}