2

我刚开始学习 Node.js,因为我发现它是创建推送通知服务器的正确工具。

所以我在这里得到了这个狡猾的脚本,我使用一些互联网教程一起破解了:

// ------------------------------------------------------------
// HTTP Server stack using Node.js
// ------------------------------------------------------------

// import the HTTP module that ships with Node.js 
var http = require("http");
var url = require("url");

// push notification helper modules
var crypto = require("crypto");
var tls = require("tls");
var fs = require("fs");
var stream = null;

process.chdir(__dirname);

//connect(function(){});

function connect(next)
{
    filepath = 'cert_and_key_dev.pem';  
    var certPem = fs.readFileSync(filepath, encoding='ascii');
    var keyPem = fs.readFileSync(filepath, encoding='ascii');
    var options = { key: keyPem, cert: certPem };

    var apnshost_dev = 'gateway.sandbox.push.apple.com';
    var apnsport = 2195;

    stream = tls.connect(apnsport, apnshost_dev, options, function() {
        // connected
        console.log('I am connected to APNS, WOO HOO!');
        //next(!stream.authorized, stream);

        pushTest();
    });
}

function pushTest()
{
    var pushnd = { aps: { alert: 'This is a test' }, customParam: { foo: 'bar' } } // aps is required
    var hextoken = '...my push token here....';

    // construct the protocol data unit
    var payload = JSON.stringify(pushnd);
    var payloadlen = Buffer.byteLength(payload, 'utf-8'); // encoded UTF-8 string length, max 255 bytes
    var tokenlen = 32;
    var buffer = new Buffer(1 + 4 + 4 + 2 + tokenlen + 2 + payloadlen);
    var i = 0;
    buffer[i++] = 1; // command
    var msgid = 0xbeefcace; // message identifier, can be left 0
        buffer[i++] = msgid >> 24 & 0xFF;
    buffer[i++] = msgid >> 16 & 0xFF;
        buffer[i++] = msgid >> 8 & 0xFF;
    buffer[i++] = msgid & 0xFF;

    // expiry in epoch seconds (1 hour)
    var seconds = Math.round(new Date().getTime() / 1000) + 1*60*60; // expire in epoch seconds (1 hour)
    buffer[i++] = seconds >> 24 & 0xFF;
    buffer[i++] = seconds >> 16 & 0xFF;
    buffer[i++] = seconds >> 8 & 0xFF;
    buffer[i++] = seconds & 0xFF;

    buffer[i++] = tokenlen >> 8 & 0xFF; // token length
    buffer[i++] = tokenlen & 0xFF;
    token = hextobin(hextoken);
    token.copy(buffer, i, 0, tokenlen);
    i += tokenlen;

    buffer[i++] = payloadlen >> 8 & 0xFF; // payload length
    buffer[i++] = payloadlen & 0xFF;

    payload = Buffer(payload);
    payload.copy(buffer, i, 0, payloadlen);

    var j = 0;

    // try sending multiple copies of the notification in one stream
    for(var j = 0; j < 5; j++)
    {
        var writable = stream.write(buffer); // write push notification to socket stream (send it out)
    }

    console.log('Test notification sent!');

    // Handling error messages
    stream.on('data', function(data) {
        var command = data[0] & Ox0FF // always 8
        var status = data[1] & 0x0FF // error code
        var msgid = (data[2] << 24) + (data[3] << 16) + (data[4] << 8) + (data[5]);

        console.log(command + ':' + status + ':' + msgid);
    });
}

function hextobin(hexstr)
{
    buf = new Buffer(hexstr.length / 2);

    for(var i = 0; i < hexstr.length/2; i++)
    {
        buf[i] = (parseInt(hexstr[i * 2], 16) << 4) + (parseInt(hexstr[i * 2 + 1], 16));
    }

    return buf;
}

function start(route, handle)
{
    function onRequest(request, response) 
    {
        // using url module to handle routing and mapping
        // of each request
        var pathname = url.parse(request.url).pathname;
        console.log("Request for " + pathname + " received.");
        route(handle, pathname, response, request);

        /*
        request.setEncoding("utf8");


        // POST data callback function for each chunk of data
        request.addListener("data", function(postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk '" +
            postDataChunk + "' .");
        });

        // POST data callback function for completion of data download
        request.addListener("end", function() {
            route(handle, pathname, response, postData);
        });

        // depedency injective response object to allow
        // request handler to use it
        route(handle, pathname, response);
        */
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started.");
}

exports.start = start;
exports.connect = connect;

在 for 循环中:

// try sending multiple copies of the notification in one stream
for(var j = 0; j < 5; j++)
{
    var writable = stream.write(buffer); // write push notification to socket stream (send it out)
}

我最多只能收到 5 个通知中的 2 个。

我做错了吗?

4

1 回答 1

1

Apple 会检查推送消息是否重复,以防止垃圾邮件。
尝试更改消息内容。

于 2013-09-07T17:53:33.993 回答