如何使用MobileFirst Foundation 8中的REST API从Node.js发送推送通知?

谁知道我在Node.js服务器上实现这个错误是什么?

该参数是有效的,它在我的本地Mac上与海报。 Node.js和MFP 8 Beta在Mac上本地运行。

在这里输入图像说明

这里是server.js文件的代码,步骤是:

  1. 准备标题
  2. MFP设置
  3. 创build发布选项
  4. 为MFP推送创buildJSON对象
  5. 使用http进行POST调用
  6. 编写json推送数据

    app.post('/award', function(req, res){ var notificationMessage = req.body.message; // prepare the header // MFP Settings var theAuthorization = "Bearer eyJhbGciOiJSUzI1NiIsImp…….Wg"; var appname = 'com.ionicframework.checkapp'; var http = require('http'); var theHost = 'localhost'; // here only the domain name var thePort = 9080; var thePath = 'imfpush/v1/apps/' + appname + '/messages'; var theMethode = 'POST'; var postheaders = { 'Authorization' : theAuthorization , 'Content-Type' : 'application/json' }; // the post options var optionspost = { host : theHost, port : thePort, path : thePath, method : theMethode, headers : postheaders }; // create the JSON object for MFP Push var jsonObject = JSON.stringify({"message":{"alert" :notificationMessage}}); console.info('---> Options prepared:'); console.info(optionspost); console.info('---> Do the POST call'); // do the POST call using http var reqPost = http.request(optionspost, function(res) { console.log("---> statusCode: ", res.statusCode); console.log("---> headers: ", res.headers); res.on('data', function(d) { console.info('---> POST result:\n'); process.stdout.write(d); console.info('\n\n---> POST completed'); }); }); // write the json Push Data reqPost.write(jsonObject); reqPost.end(); reqPost.on('error', function(e) { console.error(e); }); res.end("OK"); }); 

我得到了statusCode:400这是控制台输出:

准备的选项:

 { host: 'localhost', port: 9080, path: 'imfpush/v1/apps/com.ionicframework.checkapp/messages', method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer eyJhbGciOiJSUzI1NiIsImp3ayI6......DjbgjqVz5JFVcT8i5k_JWg' } } ---> Do the POST call ---> statusCode: 400 ---> headers: { 'content-length': '0', connection: 'Close', date: 'Wed, 22 Jun 2016 12:02:50 GMT' } 

这些是我的信息来源: https : //isolaso​​ftware.it/2012/05/28/call-rest-api-with-node-js/和https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0 /通知/发送推的通知/

感谢@Idan的文本validation和@Nathan的评论。

我发现这个问题,现在它工作。 我改变了请求准备的顺序和代码的一些变化。

  1. 准备标题
  2. MFP设置
  3. 创buildMFP推送 – >上的JSON对象
  4. 创buildpost选项 – > 向下移动
  5. 使用http进行POST调用
  6. 编写json推送数据

代码更改:

  1. 在头中插入'Content-Length':Buffer.byteLength(jsonObject)
  2. 在path中添加一个斜杠var thePath ='/ imfpush / v1 / apps /'+ appname +'/ messages';