簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM