如何在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服务器上实现这一点有什么不妥?
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:
- 准备标题
- MFP设置
- 创建帖子选项
- 为MFP Push创建JSON对象
- 使用http $ b进行POST呼叫$ b
-
写json推送数据
- prepare the header
- MFP Settings
- create post options
- create the JSON object for MFP Push
- Do the POST call using http
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://isolasoftware.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.
- 准备标题
- MFP设置
- 为MFP Push创建JSON对象 - > 已向上移动
- 创建帖子选项 - > 向下移动
- 使用http进行POST调用
- 写入json推送数据
- prepare the header
- MFP Settings
- create the JSON object for MFP Push -> Moved UP
- create post options -> Moved Down
- Do the POST call using http
- write the json Push Data
代码更改:
- 插入'内容长度':标题内的Buffer.byteLength(jsonObject)。
- 在路径中添加斜杠 var thePath ='/ imfpush / v1 / apps /'+ appname +'/ messages';
- Insert 'Content-Length': Buffer.byteLength(jsonObject) inside the header.
- Adding a slash to the path var thePath = '/imfpush/v1/apps/' + appname + '/messages';
这篇关于如何在MobileFirst Foundation 8中使用REST API从Node.js发送推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!