如何在MobileFirst Foundation 8中使用REST API从Node.js发送推送通知? [英] How to send a push notification from Node.js using REST API in MobileFirst Foundation 8?

查看:124
本文介绍了如何在MobileFirst Foundation 8中使用REST API从Node.js发送推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁知道我在Node.js服务器上实现这一点有什么不妥?

Who knows what did I wrong in implementing this on a Node.js server?

参数有效,并且可以在本地Mac上使用Poster。
Node.js和MFP 8 Beta在Mac上本地运行。

The parameter were valid and it worked with the Poster on my local Mac. Node.js and MFP 8 Beta are running locally on the Mac.

以下是服务器的代码。 js文件和步骤是:

Here is the code for the server.js file and the steps are:


  1. 准备标题

  2. MFP设置

  3. 创建帖子选项

  4. 为MFP Push创建JSON对象

  5. 使用http
  6. $ b进行POST呼叫$ b
  7. 写json推送数据

  1. prepare the header
  2. MFP Settings
  3. create post options
  4. create the JSON object for MFP Push
  5. Do the POST call using http
  6. write the json Push Data

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,这是控制台输出:

I get the statusCode:400 and this is console output:

准备的选项:

{ 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 / notifications / sending-push-notifications /

These were my information sources: https://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/ and https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/8.0/notifications/sending-push-notifications/

推荐答案

感谢@Idan进行文本验证,感谢@Nathan进行评论。

Thanks @Idan for the text validation and @Nathan for the comment.

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

I found the problem and now it works. I changed the order of the request preparation and some changes in the code.


  1. 准备标题

  2. MFP设置

  3. 为MFP Push创建JSON对象 - > 已向上移动

  4. 创建帖子选项 - > 向下移动

  5. 使用http进行POST调用

  6. 写入json推送数据

  1. prepare the header
  2. MFP Settings
  3. create the JSON object for MFP Push -> Moved UP
  4. create post options -> Moved Down
  5. Do the POST call using http
  6. write the json Push Data

代码更改:


  1. 插入'内容长度':标题内的Buffer.byteLength(jsonObject)

  2. 在路径中添加斜杠 var thePath ='/ imfpush / v1 / apps /'+ appname +'/ messages';

  1. Insert 'Content-Length': Buffer.byteLength(jsonObject) inside the header.
  2. Adding a slash to the path var thePath = '/imfpush/v1/apps/' + appname + '/messages';

这篇关于如何在MobileFirst Foundation 8中使用REST API从Node.js发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆