api/subscriptions/index.js
/**
 * Node.js/Connect style handler for handling push notifications.
 * Note that Connect is more limited that express, e.g. the request and
 * response objects are the raw Node.js HTTP Request & Response instances.
 */
Node.js/Connect style handler for handling push notifications.
Note that Connect is more limited that express, e.g. the request and
response objects are the raw Node.js HTTP Request & Response instances.
Node . js / Connect style handler for handling push notifications . Note that Connect is more limited that express e . g . the request and response objects are the raw Node . js HTTP Request & Response instances .
async function handlePUSH(req, res, next) {
  if (req.method !== 'POST') {
    console.warn(`Method '${req.method}' not supported. '`);
    res.writeHead(200, {'Content-Type': 'application/json'});
    return res.end(JSON.stringify({message: `${req.method} not supported`}));
  }
  console.log(`Handling request with body ${JSON.stringify(req.body)}`);
  try {
    const token = req.body.token;
    const program = req.body.program;
    const subscription = {
  		endpoint: token.endpoint,
  		keys: {
  			p256dh: token.keys.p256dh,
  			auth: token.keys.auth,
  		},
  	};
    const options = {
      TTL: 60*60, 
      gcmAPIKey: config.gcmAPIKey,
      vapidDetails: config.vapidDetails,
    };
    const immediateNotification = {
      title: 'Muistutus tilattu',
      body: `Vinkkaamme heti, kun ${program.title} on saatavilla Areenasta`,
      icon: '/images/touch/chrome-touch-icon-192x192.png',
    };
    await webPush.sendNotification(subscription, JSON.stringify(immediateNotification), options);
    const delayedNotification = {
      title: `${program.title} saatavana Areenassa`,
      body: `Ohjelma on nyt katsottavissa - klikkaa ja katso: ${program.title}`,
      icon: '/images/touch/chrome-touch-icon-192x192.png',
      actions: [{
        title: 'Katso',
        action: '/programs/${program.id}',
      }],
    };
    setTimeout(() => {
      webPush.sendNotification(subscription, JSON.stringify(delayedNotification), options);
    }, 15 * 1000);
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify({message: 'Subscription created'}));
    return;
  } catch (error) {
    console.error(`Caught an error: ${error.message}`);
    console.error(error.stackTrace);
    res.writeHead(500, {'Content-Type': 'application/json'});
    res.end(JSON.stringify({message: `Subscription failed: ${error.message}`}));
    return;
  }
}
async function handlePUSH ( req , res , next ) { if ( req . method !== 'POST' ) { console . warn ( ` ${ req . method } ` ) ; res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ; return res . end ( JSON . stringify ( { message : ` ${ req . method } ` } ) ) ; } console . log ( ` ${ JSON . stringify ( req . body ) } ` ) ; try { const token = req . body . token ; const program = req . body . program ; const subscription = { endpoint : token . endpoint , keys : { p256dh : token . keys . p256dh , auth : token . keys . auth , } , } ; const options = { TTL : 60 * 60 , gcmAPIKey : config . gcmAPIKey , vapidDetails : config . vapidDetails , } ; const immediateNotification = { title : 'Muistutus tilattu' , body : ` ${ program . title } ` , icon : '/images/touch/chrome-touch-icon-192x192.png' , } ; await webPush . sendNotification ( subscription , JSON . stringify ( immediateNotification ) , options ) ; const delayedNotification = { title : ` ${ program . title } ` , body : ` ${ program . title } ` , icon : '/images/touch/chrome-touch-icon-192x192.png' , actions : [ { title : 'Katso' , action : '/programs/${program.id}' , } ] , } ; setTimeout ( ( ) => { webPush . sendNotification ( subscription , JSON . stringify ( delayedNotification ) , options ) ; } , 15 * 1000 ) ; res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ; res . end ( JSON . stringify ( { message : 'Subscription created' } ) ) ; return ; } catch ( error ) { console . error ( ` ${ error . message } ` ) ; console . error ( error . stackTrace ) ; res . writeHead ( 500 , { 'Content-Type' : 'application/json' } ) ; res . end ( JSON . stringify ( { message : ` ${ error . message } ` } ) ) ; return ; } }
Node.js/Connect style handler for handling push notifications.
Node . js / Connect style handler for handling push notifications .
// Only respond for POST requests // TODO Validate the input // Send a registration, with the keys for GCM // One hour validity // Send a notification with sample payload to demonstrate the capabilities // Simulate a real push notification by sending this sample 15 seconds from // subscribing. // Send the normal HTTP response