2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
2018-02-09 07:50:51 +00:00
|
|
|
* RemoteMessage representation wrapper
|
2017-12-04 12:07:41 +00:00
|
|
|
*/
|
2017-03-30 15:25:27 +00:00
|
|
|
import { isObject, generatePushID } from './../../utils';
|
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
import type {
|
|
|
|
NativeInboundRemoteMessage,
|
|
|
|
NativeOutboundRemoteMessage,
|
|
|
|
} from './types';
|
2018-02-09 07:50:51 +00:00
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
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;
|
2017-12-04 12:07:41 +00:00
|
|
|
|
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;
|
2018-03-05 08:28:13 +00:00
|
|
|
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;
|
2017-03-30 15:25:27 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-05 08:28:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param collapseKey
|
2017-03-30 15:25:27 +00:00
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2018-02-09 16:59:29 +00:00
|
|
|
setCollapseKey(collapseKey: string): RemoteMessage {
|
|
|
|
this._collapseKey = collapseKey;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param data
|
|
|
|
* @returns {RemoteMessage}
|
2017-03-30 15:25:27 +00:00
|
|
|
*/
|
2018-02-09 16:59:29 +00:00
|
|
|
setData(data: { [string]: string } = {}) {
|
2018-02-09 07:50:51 +00:00
|
|
|
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 07:50:51 +00:00
|
|
|
);
|
|
|
|
}
|
2018-02-09 16:59:29 +00:00
|
|
|
this._data = data;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param messageId
|
2017-03-30 15:25:27 +00:00
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2018-02-09 16:59:29 +00:00
|
|
|
setMessageId(messageId: string): RemoteMessage {
|
|
|
|
this._messageId = messageId;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param messageType
|
2017-03-30 15:25:27 +00:00
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2018-02-09 16:59:29 +00:00
|
|
|
setMessageType(messageType: string): RemoteMessage {
|
|
|
|
this._messageType = messageType;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:36:59 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param to
|
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
|
|
|
setTo(to: string): RemoteMessage {
|
|
|
|
this._to = to;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param ttl
|
2017-03-30 15:25:27 +00:00
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2018-02-09 16:59:29 +00:00
|
|
|
setTtl(ttl: number): RemoteMessage {
|
|
|
|
this._ttl = ttl;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-23 16:11:59 +00:00
|
|
|
build(): NativeOutboundRemoteMessage {
|
2018-03-22 12:46:37 +00:00
|
|
|
if (!this._data) {
|
2018-02-09 07:50:51 +00:00
|
|
|
throw new Error('RemoteMessage: Missing required `data` property');
|
2018-03-22 12:46:37 +00:00
|
|
|
} else if (!this._messageId) {
|
2018-02-09 07:50:51 +00:00
|
|
|
throw new Error('RemoteMessage: Missing required `messageId` property');
|
2018-03-22 12:46:37 +00:00
|
|
|
} else if (!this._to) {
|
2018-02-09 07:50:51 +00:00
|
|
|
throw new Error('RemoteMessage: Missing required `to` property');
|
2018-03-22 12:46:37 +00:00
|
|
|
} else if (!this._ttl) {
|
2018-02-09 07:50:51 +00:00
|
|
|
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,
|
2018-02-09 07:50:51 +00:00
|
|
|
};
|
2017-03-30 15:25:27 +00:00
|
|
|
}
|
|
|
|
}
|