mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-05-28 15:44:46 +08:00
[js][ios][android][messaging] full implementation of upstream send including RemoteMessage builder
This commit is contained in:
80
lib/modules/messaging/RemoteMessage.js
Normal file
80
lib/modules/messaging/RemoteMessage.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { isObject, generatePushID } from './../../utils';
|
||||
|
||||
export default class RemoteMessage {
|
||||
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() {
|
||||
return Object.assign({}, this.properties);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user