Anyone using Node.js with Amazon SNS and Apple Push Notifications?

后端 未结 3 628
心在旅途
心在旅途 2021-01-30 18:36

I\'m looking for examples of using node.js with Amazon SNS and Apple APN push notifications. We use Amazon for our hosting, and I have used SNS before, it\'s pretty simple. But

3条回答
  •  难免孤独
    2021-01-30 19:02

    var AWS = require('aws-sdk');
    var express = require('express');
    var app = express();
    
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
     IdentityPoolId: 'add IdentityPoolId'
    });
    
    AWS.config.region = 'add region';
    
    var sns = new AWS.SNS();
    
       sns.createPlatformEndpoint({
        PlatformApplicationArn: 'add platform application arn',
        Token: 'add device token'
       }, function (err, data) {
        if (err) {
         console.log("errorMessage" + err.stack);
         return;
        }
    
        var endpointArn = data.EndpointArn;
        var payload = {
         default: 'Hello World',
         APNS: {
          aps: {
           alert: 'Hello World',
           sound: 'default',
           badge: 1
          }
         }
        };
    
        // first have to stringify the inner APNS object...
        payload.APNS = JSON.stringify(payload.APNS);
    
        // then have to stringify the entire message payload
        payload = JSON.stringify(payload);
    
        console.log('sending push');
        sns.publish({
         Message: payload,
         MessageStructure: 'json',
         TargetArn: endpointArn
        }, function (err, data) {
         if (err) {
          console.log(err.stack);
          return;
         }
    
         console.log('push sent');
         console.log(data);
        });
       });
    
    var server = app.listen(8081, function () {
       var host = server.address().address
       var port = server.address().port
    
       console.log("Example app listening at http://%s:%s", host, port)
    })
    

提交回复
热议问题