简体   繁体   中英

Push Notification not received - AWS SNS and Expo

I am trying to use AWS SNS to send my push notifications. I am just testing for now. I have already created the platform and the endpoint in SNS. When I send a push notification from the SNS console I received it without a problem. 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. I am testing the app on my real Android phone. 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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