@superhuman/push-receiver
Version:
A module to subscribe to GCM/FCM and receive notifications within a node process.
43 lines (38 loc) • 1.17 kB
JavaScript
const register = require('./register');
const Client = require('./client.js');
module.exports = {
listen,
register,
};
async function listen(credentials, notificationCallback, options = {}) {
const { socketTimeout, socketKeepAliveDelay } = options;
if (!credentials) {
throw new Error('Missing credentials');
}
if (!credentials.gcm) {
throw new Error('Missing gcm object in credentials');
}
if (!credentials.gcm.androidId) {
throw new Error('Missing gcm.androidId in credentials');
}
if (!credentials.gcm.securityToken) {
throw new Error('Missing gcm.securityToken in credentials');
}
if (!credentials.keys) {
throw new Error('Missing keys object in credentials');
}
if (!credentials.keys.privateKey) {
throw new Error('Missing keys.privateKey in credentials');
}
if (!credentials.keys.authSecret) {
throw new Error('Missing keys.authSecret in credentials');
}
const client = new Client(credentials, {
persistentIds : credentials.persistentIds,
socketTimeout,
socketKeepAliveDelay,
});
client.on('ON_NOTIFICATION_RECEIVED', notificationCallback);
client.connect();
return client;
}