[英]Push Notification not received - AWS SNS and Expo
我正在尝试使用 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”的人,他在 Facebook 上给了我这个问题的答案。它是这样工作的:
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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.