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-09 07:50:51 +00:00
|
|
|
type NativeRemoteMessage = {
|
|
|
|
collapseKey?: string,
|
|
|
|
data: { [string]: string },
|
|
|
|
messageId: string,
|
|
|
|
messageType?: string,
|
|
|
|
to: string,
|
|
|
|
ttl: number,
|
|
|
|
};
|
|
|
|
|
2017-03-30 15:25:27 +00:00
|
|
|
export default class RemoteMessage {
|
2018-02-09 07:50:51 +00:00
|
|
|
collapseKey: string | void;
|
|
|
|
data: { [string]: string };
|
|
|
|
messageId: string;
|
|
|
|
messageType: string | void;
|
|
|
|
to: string;
|
|
|
|
ttl: number;
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2018-02-09 07:50:51 +00:00
|
|
|
constructor() {
|
|
|
|
this.data = {};
|
|
|
|
this.messageId = generatePushID();
|
|
|
|
this.ttl = 3600;
|
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 07:50:51 +00:00
|
|
|
withCollapseKey(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 07:50:51 +00:00
|
|
|
withData(data: Object = {}) {
|
|
|
|
if (!isObject(data)) {
|
|
|
|
throw new Error(
|
|
|
|
`RemoteMessage:withData expects an object but got type '${typeof data}'.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
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 07:50:51 +00:00
|
|
|
withMessageId(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 07:50:51 +00:00
|
|
|
withMessageType(messageType: string): RemoteMessage {
|
|
|
|
this.messageType = messageType;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2018-02-09 07:50:51 +00:00
|
|
|
* @param ttl
|
2017-03-30 15:25:27 +00:00
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2018-02-09 07:50:51 +00:00
|
|
|
withTtl(ttl: number): RemoteMessage {
|
|
|
|
this.ttl = ttl;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-02-09 07:50:51 +00:00
|
|
|
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,
|
|
|
|
};
|
2017-03-30 15:25:27 +00:00
|
|
|
}
|
|
|
|
}
|