【发布时间】:2016-06-22 13:22:21
【问题描述】:
谁知道我在 Node.js 服务器上实现这个时做错了什么?
该参数有效,并且可以与我本地 Mac 上的海报一起使用。 Node.js 和 MFP 8 Beta 在 Mac 上本地运行。
这里是 server.js 文件的代码,步骤如下:
- 准备标题
- 多功能一体机设置
- 创建帖子选项
- 为 MFP Push 创建 JSON 对象
- 使用 http 进行 POST 调用
-
编写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/
【问题讨论】:
-
一些注意事项:您可以使用 Wireshark 来检查网络上发生的情况吗?查看响应的详细信息? - Bearer 令牌仅在一定时间内有效,您可以在 Postman 中检查它是否仍然有效? - 我注意到您的 json 正文没有“目标”收件人 - 谁应该收到此通知?
-
另外我认为
path需要以/开头
标签: node.js push-notification ibm-mobilefirst