amazon-web-services - 未收到推送通知 - AWS SNS 和 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
});
推荐阅读
- spring-boot - Spring WebClient 执行 https 调用
- python - AssertionError:ForeignKey 的第一个参数必须是模型、模型名称或字符串“self”
- ruby-on-rails - 通过 Ruby on Rails 和 oracle-enhanced_adapter 使用 Oracle JDBC 语句缓存
- c# - 在 C# 中使用可为空引用类型的对象验证
- javascript - 如何打印基于对象层次结构的 HTML 标记?
- html - 如何在悬停时显示子菜单容器
- java - WELD - 从扫描 / beans.xml 中排除应用程序范围的 bean
- java - 连续二进制数的连接,模运算讨论行为
- javascript - 使用 XMLHttpRequest 和 PHP 将图像保存到 IIS 服务器
- r - 在 ggplot 中根据线型和颜色添加图例