Help with Node.Js

0

I am doing a mini web server in Node.js in order to send push notifications to mobile devices .

I have the following code:

var apn = require('apn');

// Set up apn with the APNs Auth Key
var apnProvider = new apn.Provider({  
 token: {
    key: 'apns.p8', // Path to the key p8 file
    keyId: 'ABCDE12345', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
    teamId: 'ABCDE12345', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
},
production: false // Set to true if sending a notification to a production iOS app
});

// Enter the device token from the Xcode console
var deviceToken = '5311839E985FA01B56E7AD74444C0157F7F71A2745D0FB50DED665E0E882';

// Prepare a new notification
var notification = new apn.Notification();

// Specify your iOS app's Bundle ID (accessible within the project editor)
notification.topic = 'my.bundle.id';

// Set expiration to 1 hour from now (in case device is offline)
notification.expiry = Math.floor(Date.now() / 1000) + 3600;

// Set app badge indicator
notification.badge = 3;

// Play ping.aiff sound when the notification is received
notification.sound = 'ping.aiff';

// Display the following message (the actual notification text, supports emoji)
notification.alert = 'Hello World \u270C';

// Send any extra payload data with the notification which will be accessible to your app in didReceiveRemoteNotification
notification.payload = {id: 123};

// Actually send the notification
apnProvider.send(notification, deviceToken).then(function(result) {  
// Check the result for any failed devices
console.log(result);
});

How can I do to generate a function through which mobile devices can connect to this server and send the variable deviceToken in order not to have to enter the token manually but to be automatic?

Greetings!

    
asked by Francisco Pérez 09.02.2017 в 22:27
source

1 answer

0
  

How can I do to generate a function through which mobile devices can connect to this server and send variable deviceToken ?

Creating an HTTP server so that clients can make requests. There are many ways to do it in Node.js . You can use the module http of Node.js or using a framework like Express , Koa , etc.

Using http

const http = require('http');


http.createServer(function (req, res) {
  if (req.method.toLowerCase() === 'get') {
    res.setHeader('Content-Type', 'text/plain');
    res.end(deviceToken); // envias el token en cada petición
  }
});

Using Express

const express = require('express');


let app = express();
app.get('/', (req, res) => {
  res.type('text/plain');
  res.send(deviceToken);
});

Using Koa

const Koa = require('koa');
const KoaRouter = require('koa-router');


let app = new Koa();
let router = new KoaRouter();

router.get('/', async (ctx) => {
  ctx.body = deviceToken;
});

app.use(router.routes());

Note: Keep in mind that this is not safe since anyone who knows the address can make a request and get the token. You must implement authentication.

    
answered by 10.02.2017 / 16:05
source