简体   繁体   English

未收到推送通知 - AWS SNS 和 Expo

[英]Push Notification not received - AWS SNS and Expo

I am trying to use AWS SNS to send my push notifications.我正在尝试使用 AWS SNS 发送我的推送通知。 I am just testing for now.我现在只是在测试。 I have already created the platform and the endpoint in SNS.我已经在 SNS 中创建了平台和端点。 When I send a push notification from the SNS console I received it without a problem.当我从 SNS 控制台发送推送通知时,我毫无问题地收到了它。 This is how I am sending it from there:这就是我从那里发送它的方式:

{"GCM": "{ \"notification\": {\"title\": \"Test\", \"body\": \"It works\" } }"}

Now, I am trying to send push notifications programmatically using the Javascript SDK, but I can't get it to work correctly.现在,我正在尝试使用 Javascript SDK 以编程方式发送推送通知,但我无法使其正常工作。 I am testing the app on my real Android phone.我正在我的真实 Android 手机上测试该应用程序。 Which connects to my server app hosted locally.它连接到我在本地托管的服务器应用程序。 When the app opens, I press a button that sends an http request to my local server app, which then tries to send the push notification.当应用程序打开时,我按下一个按钮,向我的本地服务器应用程序发送一个 http 请求,然后它会尝试发送推送通知。

What is strange is that I get the message that the push notification was sent, but I never receive it on my phone:奇怪的是,我收到推送通知已发送的消息,但我从未在手机上收到它:

Push notification sent successfully!!!!  {
  ResponseMetadata: { RequestId: 'XXX-5913-996f-b8ec0c010eac' },
  MessageId: 'XXX-5308-98e3-0689b03df4b7'
}

This is the code that I have in my local server app:这是我在本地服务器应用程序中的代码:

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
    }); 
}

I have tried to change the formatting of the Message and still nothing.我试图更改消息的格式,但仍然没有。 What am I doing wrong?我究竟做错了什么?

Thanks to a person called "Jitesh Prajapati" who gave me the answer to this on Facebook. This is how it works:感谢一个叫“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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM