Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.post('/send_notification', function (req, res) {
if (!req.body) {
res.status(400);
}
var message = new gcm.Message();
var temp = req.body.endpoint.split('/');
var regTokens = [temp[temp.length - 1]];
var sender = new gcm.Sender('AIzaSyCjrU5SqotSg2ybDLK_7rMMt9Rv0dMusvY'); //Replace with your GCM API key
// Now the sender can be used to send messages
sender.send(message, { registrationTokens: regTokens }, function (error, response) {
if (error) {
console.error(error);
res.status(400);
}
else {
console.log(response);
res.status(200);
}
});
});
app.post('/send_notification', function (req, res) {
if (!req.body) {
res.status(400);
}
var message = new gcm.Message();
var temp = req.body.endpoint.split('/'); //End point send from client
var regTokens = [temp[temp.length - 1]];
var sender = new gcm.Sender("<%= apiKey %>"); //API key
// Now the sender can be used to send messages
sender.send(message, { registrationTokens: regTokens }, function (error, response) {
if (error) {
console.error(error);
res.status(400);
}
else {
console.log(response);
res.status(200);
}
});
});
notifyUsers: function(req, res){
var sender = new gcm.Sender(secrets.fcm);
// Prepare a message to be sent
var message = new gcm.Message({
notification: {
title: "Hello, World",
icon: "ic_launcher",
body: "Click to see the latest commit"
}
});
User.find({}, function(err, users) {
// user subscription ids to deliver message to
var user_ids = _.map(users, 'user_id');
console.log("User Ids", user_ids);
return new Promise(function (resolve, reject) {
var registrationTokens = (typeof tokens === 'undefined' ? 'undefined' : _typeof(tokens)) === 'object' ? tokens : [tokens];
var sender = new _nodeGcm.Sender(config.androidSenderAPIKey);
var notification = Android.genMessage(body.title, body.message, body.payload, config.bundle);
sender.send(notification, { registrationTokens: registrationTokens }, function (err) {
if (err) reject(err);else resolve();
});
});
}
return new Promise((resolve, reject) => {
const registrationTokens = typeof tokens === 'object' ? tokens : [tokens];
const sender = new Sender(config.androidSenderAPIKey);
const notification = Android.genMessage(
body.title,
body.message,
body.payload,
config.bundle
);
sender.send(notification, { registrationTokens }, err => {
if (err) reject(err);
else resolve();
});
});
}
function sendNotifications(registrationTokens) {
var message = new gcm.Message();
message.addData({
key1: 'Welcome to push notification',
key2: 'How do you feel now?'
});
var sender = new gcm.Sender(process.env.GOOGLE_API_KEY);
sender.send(message, { registrationTokens: registrationTokens }, function (err, response) {
if(err)
console.log(err);
else
console.log(response);
});
}
this.sendEvent = function(title, id, gid){
console.log("Sending GCM message about "+title);
var message = new gcm.Message();
var sender = new gcm.Sender(config.gcmApiKey);
var ids = [];
//Add the title and ID data
message.addData('title', title);
message.addData('id', id);
//This allows the GCM servers to deliver only 1 message per key if a device is offline or something
message.collapseKey = 'gid'+gid;
//Not sure what unit this is, but 2 sounds like a good choice
message.timeToLive = 2;
//Need to get all the IDs now, up to 1000 at a time
if(config.noDB){
for(var i = 0; i < registeredIds.length && i < 1000; i++){
ids.push(registeredIds[i].registrationId);
}
sender.send(message, ids, 5, function(result){
GcmPushManager.prototype.connect = function() {
debug('GCM : connecting to GCM platform using ', this.application);
if (this.application === null) {
throw "GcmPushManager : an application object must be provided.";
}
this.service = new gcm.Sender(this.application.key);
debug('GCM : connection established using ', this.service);
};
/**
var Gcm = exports.Gcm = function (api_key) {
this.sender = new nodegcm.Sender(api_key);
};