我正在尝试使用AWS SNS发送我的推送通知。我现在正在测试。我已经在SNS中创建了平台和端点。当我从SNS控制台发送推送通知时,我没有收到任何通知。这就是我从那里发送的方式:
{"GCM": "{ \"notification\": {\"title\": \"Test\", \"body\": \"It works\" } }"}
现在,我尝试使用Javascript SDK以编程方式发送推送通知,但无法使其正常工作。我正在我的真实Android手机上测试该应用程序。哪个连接到本地托管的我的服务器应用程序。当应用程序打开时,我按下一个按钮,将一个http请求发送到我的本地服务器应用程序,然后该应用程序尝试发送推送通知。
奇怪的是,我收到了发送了推送通知的消息,但我从未在手机上收到它:
Push notification sent successfully!!!! {
ResponseMetadata: { RequestId: 'XXX-5913-996f-b8ec0c010eac' },
MessageId: 'XXX-5308-98e3-0689b03df4b7'
}
这是我在本地服务器应用程序中拥有的代码:
module.exports.sendPushNotification = () => {
var sns = new AWS.SNS({ apiVersion: '2010-03-31', region: 'us-east-1'});
var params = {
Message: `{
"GCM": "{ \"notification\": {\"title\": \"Test\", \"body\": \"It works\" } }"
}`,
TargetArn: "######My Device ARN#######"
};
sns.publish(params, function(err, data) {
if (err) {
console.log("There was an error sending the push notification----> ", err)
} // an error occurred
else{
console.log("Push notification sent successfully!!!! ", data);
} // successful response
});
}
我试图更改消息的格式,但还是一无所获。我究竟做错了什么?
感谢一个叫“ Jitesh Prajapati”的人,他在脸书上给了我答案。它是这样工作的:
var sns = new AWS.SNS({ apiVersion: '2010-03-31', region: 'us-east-1'})
let notification = JSON.stringify({
'notification': {
'title': "Notification Title",
'body': "Your message goes here",
'data': {}
}
});
var params = {
Message: JSON.stringify({
GCM: notification
}),
MessageStructure: "json",
TargetArn: "### Your target ARN ###"
};
sns.publish(params, function(err, data) {
if (err) {
console.log("There was an error sending the push notification----> ", err)
} // an error occurred
else{
console.log("Push notification sent successfully!!!! ", data);
} // successful response
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句