2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Remote message representation wrapper
|
|
|
|
*/
|
2017-03-30 15:25:27 +00:00
|
|
|
import { isObject, generatePushID } from './../../utils';
|
|
|
|
|
|
|
|
export default class RemoteMessage {
|
2017-12-04 12:07:41 +00:00
|
|
|
properties: Object;
|
|
|
|
|
|
|
|
constructor(sender: string) {
|
2017-03-30 15:25:27 +00:00
|
|
|
this.properties = {
|
|
|
|
id: generatePushID(),
|
|
|
|
ttl: 3600,
|
|
|
|
// add the googleapis sender id part if not already added.
|
2017-04-03 09:33:18 +00:00
|
|
|
sender: `${sender}`.includes('@') ? sender : `${sender}@gcm.googleapis.com`,
|
2017-03-30 15:25:27 +00:00
|
|
|
type: 'remote',
|
|
|
|
data: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param ttl
|
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
setTtl(ttl: number): RemoteMessage {
|
2017-03-30 15:25:27 +00:00
|
|
|
this.properties.ttl = ttl;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
*/
|
|
|
|
setId(id: string): RemoteMessage {
|
2017-04-03 09:33:18 +00:00
|
|
|
this.properties.id = `${id}`;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
|
|
|
setType(type: string): RemoteMessage {
|
2017-04-03 09:33:18 +00:00
|
|
|
this.properties.type = `${type}`;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
|
|
|
setCollapseKey(key: string): RemoteMessage {
|
2017-04-03 09:33:18 +00:00
|
|
|
this.properties.collapseKey = `${key}`;
|
2017-03-30 15:25:27 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* @returns {RemoteMessage}
|
|
|
|
*/
|
|
|
|
setData(data: Object = {}) {
|
|
|
|
if (!isObject(data)) {
|
|
|
|
throw new Error(`RemoteMessage:setData expects an object as the first parameter 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]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
toJSON(): Object {
|
2017-03-30 15:25:27 +00:00
|
|
|
return Object.assign({}, this.properties);
|
|
|
|
}
|
|
|
|
}
|