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

87 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
* @flow
* Remote message representation wrapper
*/
import { isObject, generatePushID } from './../../utils';
export default class RemoteMessage {
properties: Object;
constructor(sender: string) {
this.properties = {
id: generatePushID(),
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
* @returns {RemoteMessage}
*/
setTtl(ttl: number): RemoteMessage {
this.properties.ttl = ttl;
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;
}
/**
*
* @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;
}
toJSON(): Object {
return Object.assign({}, this.properties);
}
}