有人在 Amazon SNS 和 Apple 推送通知中使用 Node.js 吗? [英] Anyone using Node.js with Amazon SNS and Apple Push Notifications?

查看:29
本文介绍了有人在 Amazon SNS 和 Apple 推送通知中使用 Node.js 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将 node.js 与 Amazon SNS 和 Apple APN 推送通知结合使用的示例.我们使用亚马逊进行托管,我之前使用过 SNS,非常简单.但是他们提供的推送通知示例是针对 Java 的,而没有针对 Node.js 的示例.像往常一样,他们很困惑,我希望减少我的研究和时间.不可能那么难.我也想知道他们如何处理错误,以及沙箱和生产之间的差异.Apple 在这两种环境中的反应不同,不会像在生产环境中那样在沙盒中失败.

I'm looking for examples of using node.js with Amazon SNS and Apple APN push notifications. We use Amazon for our hosting, and I have used SNS before, it's pretty simple. But the examples they have for push notifications are for java, and there is no examples for Node. It's confusing, as usual with them, and I'm hoping to cut my research and time spent short. It can't be that hard. I'm also wondering how they deal with errors, and the differences between the sandbox and production. Apple reacts differently between the two environments, not failing in the sandbox as they do in production.

推荐答案

结果并没有那么难,只是发现文档令人不快.您需要在控制台中为 SNS 主题创建主端点,这是迄今为止最简单的方法,包括加载证书.然后,您使用 createPlatformEnpoint 为每个设备 ID 创建一个端点.这将返回另一个特定于该设备的 SNS 主题,然后您可以使用该主题发送消息.

It ends up not being that hard, just figuring out the documentation was unpleasant. You need to create the main endpoint for the SNS topic in the console, by far the easiest way, including the loading of the certificate. You then used createPlatformEnpoint to create an endpoint for each device id. That returns another SNS topic, specific fo that device, that you then use to send the message.

因此,以下工作用于向单个客户端发送单个消息.如果你想批量发送一些东西,不确定你能不能做到.也不确定您如何处理 Apple 的反馈,您应该检查发送失败的信息.

So, the following works to send a single message to a single client. If you want send something en masse, not sure you can do that. Also not sure how you deal with Apple's feedback, which you are supposed to check for failed sends.

config = require("./config.js").config;

var token = "1234567898123456789";

var AWS = require('aws-sdk');

AWS.config.update({accessKeyId: config.AWSAccessKeyId, secretAccessKey: config.AWSSecretKey});
AWS.config.update({region: config.AWSRegion});

var sns = new AWS.SNS();

var params = {'PlatformApplicationArn':config["AWSTargetARN"],'Token':token};

var message = 'Test';
var subject = 'Stuff';

sns.createPlatformEndpoint(params,function(err,EndPointResult)
{
    var client_arn = EndPointResult["EndpointArn"];

    sns.publish({
    TargetArn: client_arn,
    Message: message,
    Subject: subject},
        function(err,data){
        if (err)
        {
            console.log("Error sending a message "+err);
        }
        else
        {
            console.log("Sent message: "+data.MessageId);

        }
    });
});

这篇关于有人在 Amazon SNS 和 Apple 推送通知中使用 Node.js 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆