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);
}
});
pushDevices.forEach(pushDevice => {
switch (pushDevice.type) {
case 'android':
// Required for fcm to be received in background
payload.title = details.title;
payload.body = details.message;
if (fcmSender) {
var message = new gcmLib.Message({
data: payload,
});
fcmSender.send(message, {
registrationTokens: [pushDevice.regId],
}, 10, (err) => {
if (err) console.log('FCM Error', err);
});
}
break;
case 'ios':
if (apn) {
apn.send({
token: pushDevice.regId,
alert: details.message,
// cert: '',
// key: ''
// });
// var iosDevice = new apn.Device('token');
// var note = new apn.Notification();
// note.badge = 3;
// note.sound = "ping.aiff";
// note.alert = send_txt;
// note.payload = {'messageFrom': 'FdAS'};
/* android */
var registrationIds = [];
var sender = new gcm.Sender('AIzaSyACRdFNYuZauygD-hhJG3rFiavqjz4ilPs'); // server key
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: send_txt
}
});
// get push list
// TODO, model 인식 안됨
var userModel = app.models.pushkey || loopback.getModelByType(app.models.pushkey);
userModel.find({}, function(err, lists){
var len = lists.length;
} else {
custom = {
data: data.custom,
};
}
custom.title = custom.title || data.title;
custom.message = custom.message || data.body;
custom.sound = custom.sound || data.sound;
custom.icon = custom.icon || data.icon;
custom.msgcnt = custom.msgcnt || data.badge;
if (opts.phonegap === true && data.contentAvailable) {
custom['content-available'] = 1;
}
const message = new gcm.Message({
// See https://developers.google.com/cloud-messaging/http-server-ref#table5
collapseKey: data.collapseKey,
priority: data.priority === 'normal' ? data.priority : 'high',
contentAvailable: data.contentAvailable || false,
delayWhileIdle: data.delayWhileIdle || false,
timeToLive: extractTimeToLive(data),
restrictedPackageName: data.restrictedPackageName,
dryRun: data.dryRun || false,
data: opts.phonegap === true ? Object.assign(custom, notification) : custom, // See https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour
notification: opts.phonegap === true ? undefined : notification,
});
let chunk = 0;
// Split in 1.000 chunks, see https://developers.google.com/cloud-messaging/http-server-ref#table1
do {
const tokens = regIds.slice(chunk * 1000, (chunk + 1) * 1000);
shovelMessenger.prototype.sendMessage = function(msgOptions) {
var _this = this;
//gcm
//preparo el mensaje
var message = new gcm.Message({
collapseKey: __('New Notifications'),
delayWhileIdle: true,
timeToLive: 3,
data: {
message: msgOptions.message,
title: msgOptions.title,
timeStamp: new Date().toISOString(),
sound: msgOptions.sound
}
});
_this.gcmSender.send(message, _this.regIdsGcm, 4, function(err, result) {
console.log(err);
console.log(result);
});
gulp.task('push:android', function() {
if (!gutil.env.apiKey || gutil.env.apiKey === true) {
throw new Error('You must specify the android Api key, refer to the documentation');
}
if (!gutil.env.deviceId || gutil.env.deviceId === true) {
throw new Error('You must specify the android ID, refer to the documentation');
}
console.log('apiKey', gutil.env.apiKey);
console.log('deviceId', gutil.env.deviceId);
var gcm = require('node-gcm')
var message = new gcm.Message({
collapseKey: 'demo',
delayWhileIdle: true,
timeToLive: 3,
data: {
key1: 'message1',
key2: 'message2'
}
});
var sender = new gcm.Sender(gutil.env.apiKey);
sender.send(message, (gutil.env.deviceId instanceof Array) ? gutil.env.deviceId : [gutil.env.deviceId], 5, function(err, result) {
if (err) {
console.error('Failed, status code', err);
} else {
console.log('Success', result);
send: opts => {
var message = new gcm.Message({
collapseKey : 'tsubakumi',
timeToLive : 3,
delayWhileIdle : false,
data : {
timestamp: +new Date(),
json: JSON.stringify(_.extend(
settings.gcm.messageTemplate, opts))
}
});
var sender = new gcm.Sender(settings.gcm.sender);
sender.send(message, settings.gcm.registrationIds, 4, (err, result) => {
console.log(result);
});
}
};
function _googlePush(devicesTokens,senderId,apiKey,data){
var defer = global.q.defer();
try{
var sender = gcm.Sender(apiKey);
var message = new gcm.Message({
priority: 'high',
contentAvailable: true,
delayWhileIdle: false,
timeToLive: 3,
dryRun: false,
data: {
key: 'Cloudboost-PN-Service'
},
notification: {
title: data.title,
icon: data.icon || 'ic_launcher',
body: data.message
}
});
//Add custom data to the message.
AndroidTransportWorker.prototype.processMessage = function(message) {
var broadcast = !!message.payload['*'];
//the default payload, used for context notifications, otherwise the real payload from message is used
var payload = {'*': {deviceTokens: [], deltas: [], applicationId: null}};
var self = this;
var gcmMessage = new gcm.Message();
async.series([
function(callback) {
if (broadcast) {
payload['*'].applicationId = message.payload['*'].applicationId;
payload['*'].deltas = message.payload['*'].deltas;
Models.Subscription.getAllDevices(message.payload['*'].applicationId, function(err, deviceIdentifiers) {
if (err) {
return callback(err);
}
if (deviceIdentifiers.android_transport) {
payload['*'].deviceTokens = deviceIdentifiers.android_transport;
}
self.sendNotification(applicationId, gcmMessage, groupedSubscription.deviceTokens, AndroidTransportWorker.sendCallback(devices, applicationId));
} else {
if (!collectedDeltas[key].length)
collectedDeltas[key].push(d);
self.storeAmazonLink(cloneObject(collectedDeltas), function(err, resourcePublicLink) {
if (err) return Models.Application.logger.error(err.message);
gcmMessage.addData({url: resourcePublicLink});
self.sendNotification(applicationId, gcmMessage, groupedSubscription.deviceTokens, AndroidTransportWorker.sendCallback(devices, applicationId));
});
}
collectedDeltas = {new: [], updated: [], deleted: []};
gcmMessage = new gcm.Message();
collectedDeltas[key].push(d);
}
c3();
}, function() {
if (collectedDeltas.new.length || collectedDeltas.updated.length || collectedDeltas.deleted.length) {