Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'node-gcm' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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('/');
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);
}
});
});
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,
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);
// 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);
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();
});
});
}
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);
});