Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const webPush = require('web-push');
const args = require('minimist')(process.argv.slice(2));
if (!args.subscription || !args.payload) {
throw new Error('Bad Input - Sorry. Include --subscription and --payload');
}
// Just makes final output a little cleaner
console.log('');
console.log('');
const subscription = JSON.parse(args.subscription);
const payload = args.payload;
webPush.setGCMAPIKey('AIzaSyBBh4ddPa96rQQNxqiq_qQj7sq1JdsNQUQ');
return webPush.sendNotification(subscription.endpoint, {
userPublicKey: subscription.keys.p256dh,
userAuth: subscription.keys.auth,
payload: payload
})
.then(result => {
console.log('Result: ', result);
})
.catch(err => {
console.log(err);
})
.then(() => {
// Just makes final output a little cleaner
console.log('');
console.log('');
const sendPushNotification = (root, args, context) => {
const { user } = context;
Users.utils.checkLoggedInAndVerified(user);
// Set web-push keys
webPush.setGCMAPIKey(gcmPrivateKey);
webPush.setVapidDetails(vapidSubject, vapidPublicKey, vapidPrivateKey);
const payload = JSON.stringify({
title: 'Welcome',
body: 'Thank you for enabling push notifications',
icon: '/android-chrome-192x192.png',
});
const options = {
TTL: 60, // time to live in seconds
};
// Gather all subscriptions from all subscribed users
const selector = { subscriptions: { $exists: true, $ne: [] } };
const projection = { fields: { _id: true, subscriptions: true } };
const users = Users.collection.find(selector, projection).fetch();
var express = require("express"),
bodyParser = require('body-parser'),
hbs = require("hbs"),
webPush = require('web-push'),
dotenv = require('dotenv'),
Twitter = require('twitter'),
EventEmitter = require('events').EventEmitter,
emitter = new EventEmitter(),
app = express(),
messageCount = 0,
endpoint, key;
dotenv.load();
webPush.setGCMAPIKey(process.env.GCM_ID);
var hashtag = process.env.HASHTAG;
var twitterClient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
// Set the templates to handlebars
app.set("views", __dirname + "/views");
app.set("view engine", "hbs");
constructor ({gcmAPIKey, vapidDetails, ttl, headers}: Object) {
this.options = {TTL: ttl, headers}
if (gcmAPIKey) {
webpush.setGCMAPIKey(gcmAPIKey)
}
if (vapidDetails) {
const {subject, publicKey, privateKey} = vapidDetails
webpush.setVapidDetails(subject, publicKey, privateKey)
}
}
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
const webpush = require('web-push');
const vapidKeys = webpush.generateVAPIDKeys();
const knex = appRequire('init/knex').knex;
const config = appRequire('services/config').all();
webpush.setGCMAPIKey(config.plugins.webgui.gcmAPIKey);
webpush.setVapidDetails('mailto:xxx@ooo.com', vapidKeys.publicKey, vapidKeys.privateKey);
exports.pushMessage = (() => {
var _ref = _asyncToGenerator(function* (title, options) {
const users = yield knex('push').select();
const arr = [];
users.forEach(function (user) {
const promise = webpush.sendNotification({
endpoint: user.endpoint,
keys: {
auth: user.auth,
p256dh: user.p256dh
}
}, JSON.stringify({ title, options })).catch(function () {
return knex('push').delete().where({ endpoint: user.endpoint });
});
sendPushNotificationsToUser: function (userId, message) {
webPush.setGCMAPIKey(Meteor.settings.private.FCM_API_KEY);
let data = WebPushSubscriptions.findOne({user_id: userId});
data.subscriptions.forEach(function (sub) {
let subscription = {
endpoint: sub.endpoint,
keys: {
p256dh: sub.key,
auth: sub.authSecret
}
};
webPush.sendNotification(subscription, message).catch((err) => {
if (err.statusCode !== 201 && err.statusCode !== 401 && err.statusCode !== 410) {
throw err;
}
});
});
}
constructor(publicKey, privateKey) {
Webpush.setGCMAPIKey('1049276846959');
Webpush.setVapidDetails(
'mailto:contact@hammerspace.co.uk',
publicKey,
privateKey
);
this.manifest = null;
}
const webpush = require('web-push');
const vapidKeys = webpush.generateVAPIDKeys();
const knex = appRequire('init/knex').knex;
const config = appRequire('services/config').all();
if(config.plugins.webgui.gcmAPIKey && config.plugins.webgui.gcmSenderId) {
webpush.setGCMAPIKey(config.plugins.webgui.gcmAPIKey);
webpush.setVapidDetails(
'mailto:xxx@ooo.com',
vapidKeys.publicKey,
vapidKeys.privateKey
);
exports.pushMessage = async (title, options) => {
const users = await knex('push').select();
const arr = [];
users.filter(f => f.userId === 1).forEach(user => {
const promise = webpush.sendNotification({
endpoint: user.endpoint,
keys: {
auth: user.auth,
p256dh: user.p256dh,
}
const config = require('config');
const webPush = require('web-push');
const Tarantool = require('../src/db/tarantool');
webPush.setGCMAPIKey(config.get('notify.gcm_key'));
function notify(account, nparams, title, body, url, pic) {
if (!nparams.keys || !nparams.keys.auth) return Promise.resolve(false);
var payload = JSON.stringify({
title,
body,
url,
icon: pic || 'https://steemit.com/favicon.ico' //FIXME domain name from config
});
return new Promise((resolve, reject) => {
webPush.sendNotification(nparams, payload).then(function() {
resolve(account);
}, function(err) {
reject(err);
});
});
function runNotificationLoop() {
webpush.setGCMAPIKey(GCMApiKey);
setInterval(
() => {
const currentDate = new Date();
const currentTime = `${_.padStart(currentDate.getHours(), 2, 0)}:${_.padStart(currentDate.getMinutes(), 2, 0)}`;
const findQuery = { time: currentTime };
SubscriptionsModel.find(findQuery).exec((err, subs) => {
subs.forEach(item => webpush.sendNotification(item.subscription));
});
},
1000 * 60
);
}