我正在创建一个将使用推送通知的Expo应用程序。我不想为此使用Expo的服务器,而是想使用AWS SNS。这就是我所做的:
当我尝试发送推送通知时,它不起作用。我尝试过:
我在想,如果我能够构建该应用程序并获取设备令牌,则可以使它正常运行,但是没有运气。对我做错了或应该做的事有任何想法吗?
答案 0 :(得分:0)
经过2天的努力,我终于做到了。我将提供我所遵循的所有步骤,希望对其他人有帮助:
"android": {
"googleServicesFile": "./google-services.json",
"package": "com.astest.mypackage",
"useNextNotificationsApi": true
},
按照说明here设置客户端应用程序。确保不包括sendPushNotification()函数,因为您实际上将使用SNS。
在registerForPushNotificationsAsync()函数中,确保使用.getDevicePushTokenAsync()而不是.getExpoPushTokenAsync()
在SNS中创建平台应用程序
在您的代码中,确保在SNS中创建一个应用程序终结点。或通过控制台进行操作。
通过使用SNS中的控制台发送测试消息来测试设置。选择您的端点,然后单击“发布消息”。
单击“每次交付的自定义有效负载”。
使用此代码:
{
"GCM": "{ \"notification\": {\"title\": \"Title of notification\", \"body\": \"It works\" } }"
}
单击发布消息。
要以编程方式发布消息,您可以执行以下操作:
var sns = new AWS.SNS({ apiVersion: '2010-03-31', region: 'us-east-1'})
let notification = JSON.stringify({
'notification': {
'title': "title of notification",
'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
});
现在,这当然仅适用于Android。但这有效!使其适应APN并不难。