ios - 如何在 Node JS 中使用 Amazon SNS 将 VoIP 推送通知发送到 iOS 设备

标签 ios node.js apple-push-notifications amazon-sns

我正在尝试使用名为 sns-mobile 的 NodeJS 包将 VoIP 推送通知从应用服务器直接发送到 iOS 设备和 Amazon SNS API .

但是,当我尝试使用以下代码发送 VoIP 推送时,这是我收到的错误消息。有人可以建议我哪里出错了,我已经花了将近半天的时间来解决这个问题。

Invalid parameter: JSON must contain an entry for 'default' or 'APNS_VOIP

var iOSApp = new SNS({
      platform: SNS.SUPPORTED_PLATFORMS.IOS,
      region: 'us-west-2',
      apiVersion: '2010-03-31',
      accessKeyId: 'XXXXXXXXXXXXX',
      secretAccessKey: 'XXXXXXXXXXXXX',
      platformApplicationArn: 'arn:aws:sns:us-west-2:3303035XXXXX:app/APNS_VOIP/VoIPPushesApp'
    }); 

iOSApp.addUser('deviceID', 
  JSON.stringify({
   "APNS_VOIP": JSON.stringify({aps:{alert:"Hello and have a good day."}})
  })
  , function(err, endpointArn) {
  if(err) {
    console.log("The Error is :****: "+JSON.stringify(err, null, 4));
    throw err;
  }


  // Send a simple String or data to the client 
  iOSApp.sendMessage(endpointArn, 'Hi There!', function(err, messageId) {
  //iOSApp.sendMessage(endpointArn, messageTest, function(err, messageId) {
    if(err) {
      console.log("The Error in end message is :****: "+JSON.stringify(err, null, 4));
      throw err;
    }
    console.log('Message sent, ID was: ' + messageId);
  });
});

最佳答案

这是使用其设备 VoIP token 向接收方设备发送 VoIP 通知的代码。当收到 VoIP 通知时,设备会调用名为 didReceiveIncomingPushWithPayload

的函数
var AWS                   =   require('aws-sdk');

// Amazon SNS module
AWS.config.update({
  accessKeyId     : 'XXXXXXXXXXXXXXXX',
  secretAccessKey : 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  region          : 'us-west-2'
});

var amazonSNS             =   new AWS.SNS();

// 1. Create Platform Endpoint
var createPlatformEndpoint = function(req, res, next){
  var deviceVoipToken = req.deviceVoipToken;  // Obtaining the device VoIP token from the request object

  amazonSNS.createPlatformEndpoint({
    // App in Sandboxmode (ie running on device directly from Xcode)
    PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP_SANDBOX/CurieVoip",
    // App in Production mode (ie running on device after archiving and installed on device with a provisioning profile)
    //PlatformApplicationArn: "arn:aws:sns:us-west-2:xxxxxxxxxxxx:app/APNS_VOIP/CurieVoip",
    Token: deviceVoipToken


  }, function(err, data) {
    if (err) {
      console.log(err.stack);
      response.json({"status": "failure", "statusCode" : 402})
      return;
    }
    var endpointArn = data.EndpointArn;
    req.endpointArn = data.EndpointArn; // Passing the EndpointArn to the next function
    next()
  })
}


// 2. Publish notification
var publishVoipNotification = function(req, res, next){
  var endpointArn = req.endpointArn;

  var payload = {
    default   : 'Hello World, default payload',
    APNS_VOIP : {
      aps: {
        alert: 'Hi there',
        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');
  amazonSNS.publish({
    MessageStructure  : 'json',
    Message           : payload,
    TargetArn         : endpointArn
  }, function(err, data) {
    if (err) {
      console.log("Error stack: "+err.stack);
      var message =  "There has been an error in Publishing message via AmazonSNS with error: "+err.stack;
      res.json({"status": "failure", "statusCode" : 402, "message" : message})
      return;
    }
    next();
  });
}


// 3. Deleting platform endpoint after sending a voipNotification; Ref: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#deleteEndpoint-property
var deleteEndpointArn = function(req, res){
  var endpointArn = req.endpointArn;
  var roomName    = req.roomName;

  var params = {
    EndpointArn: endpointArn /* required */
  };

  amazonSNS.deleteEndpoint(params, function(err, data) {
    if (err){
      var message = "Unable to deleteEndpointArn, with error: "+err.stack;
      res.json({"status": "failure", "statusCode" : 400, "message":message})
    }
    else{
      var message = "Deleted endpointArn successfully";
      res.json({"status": "success", "statusCode" : 200, "message":message})

    }
  });
}
router.post('/sendVoipNotificationToReceiver', [createPlatformEndpoint, publishVoipNotification, deleteEndpointArn]);

关于ios - 如何在 Node JS 中使用 Amazon SNS 将 VoIP 推送通知发送到 iOS 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307830/

相关文章:

ios - 如何以编程方式获取已安装的 mapbox SDK 的当前版本?

ios - iPhone 键盘尺寸

iphone - 获取相对于当前方向的 View 框架大小

javascript - 需要与导入

javascript - 从 nodejs 到 mongodb 或 mongoose 的动态数据库连接

ios - 如何在 iOS App Bundle 中创建非本地化资源

iOS - 静默推送后我需要处理时间

android - 如何处理来自移动设备的视频方向

node.js - Sequelize 删除 JSON 中不存在的行

iphone - 生成新证书后推送通知不起作用