从AWS SNS推送通知中获取其他数据

时间:2017-01-26 20:05:00

标签: android json amazon-web-services aws-lambda

我的SNS推送通知存在问题。我有以下lambda代码:

    db.scan(params, function(err, data) {
        if (err) {
          console.log(err); // an error occurred
          } 
        else {

        data.Items.forEach(function(record) {

            var receiverID = record.userDeviceToken.S;
            var message = "You have a new invitation to the event";
            var topic = "Friend's invitation";

            var eventText = JSON.stringify(event);
            console.log("Received event:", eventText);
            var sns = new AWS.SNS();
            var params = {
                Message: message, 
                Subject: "Friend's invitation",
                TargetArn: receiverID,
            };

            sns.publish(params, function(err, data) {
            if (err) {
                console.log('Failed to publish SNS message');
                context.fail(err);
            }
            else {
                console.log('SNS message published successfully');
                context.succeed(data);
            }
            });

        });

        //context.succeed(data.Items); // data.Items

      }
  });
};

现在我的目标是有时获得“主题”或“主题”,如果可能的话。我在文档中找不到它,我需要它根据发送的推送消息自定义我的通知标题(我的功能很少)。

当我使用示例亚马逊应用程序时,我在Push Listener Service中发现了这个问题:

public static String getMessage(Bundle data) {
        // If a push notification is sent as plain text, then the message appears in "default".
        // Otherwise it's in the "message" for JSON format.
        return data.containsKey("default") ? data.getString("default") : data.getString(
                "message", "");
    }

这样可行,但“数据”本身有以下数据:

Bundle[{google.sent_time=1480364966070, google.message_id=0:1480364966079787%22269524f9fd7ecd, default=You have a new invitation to the event, collapse_key=do_not_collapse}]

因此,它只是提供一些内部数据和“消息”本身。我无法访问该主题。 我的问题是:如何在Android代码中获取其他变量,以便我可以进一步使用它们?我可以通过数据包自行添加自定义变量吗?

我有JSON格式的通知。我只是想知道,唯一的方法是将我想要的数据以JSON格式放入消息中,然后相应地读取消息,或者我可以将所需的数据从lambda函数附加到推送通知中吗?

0 个答案:

没有答案