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

How to send a push notification from Node.js using REST API in MobileFirst Foundation 8?

谁知道我在 Node.js 服务器上实现它时做错了什么?

该参数有效并且它与我本地 Mac 上的海报一起使用。 Node.js 和 MFP 8 Beta 在 Mac 本地 运行。

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

  1. 准备 header
  2. 多功能一体机设置
  3. 创建post 选项
  4. 为 MFP 推送创建 JSON object
  5. 使用 http
  6. 执行 POST 调用
  7. 写入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://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/notifications/sending-push-notifications/

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

我发现了问题,现在可以了。 我更改了请求准备的顺序和代码中的一些更改。

  1. 准备 header
  2. 多功能一体机设置
  3. 为 MFP 推送创建 JSON object -> 上移
  4. 创建 post 选项 -> 下移
  5. 使用 http
  6. 执行 POST 调用
  7. 写入json推送数据

代码更改:

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