node-apn尝试发送推送通知时出错513

时间:2014-02-25 18:33:33

标签: node.js apple-push-notifications

我有一个非常好的PHP脚本并通过APNS将消息发送到我的iPhone但是当我尝试使用node-apn模块时,我得到了错误513.

在我的PHP代码中,我做的事情有点不同(见下文);即,我从设备令牌中删除空格并将其“十六进制”。我还使用了Apple推荐使用openssl cli创建的组合cert-key.pem。在node-apn中,您似乎必须单独定义证书和密钥,因此我只使用我从openssl创建的相应证书/密钥pem文件。任何想法为什么这可能不起作用?底部的节点代码。

PHP代码:

<?php

// Put your device token here (without spaces):
$deviceToken = 'mytokengoeshere';

// Put your private key's passphrase here:
$passphrase = 'secret';

// Put your alert message here:
$message = 'You have new SRC requests to approve';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default',
    'badge' => 1
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

Node.js代码:

  var message = new apn.notification();

    message.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
    message.badge = count;
    message.sound = "ping.aiff";
    message.setAlertText("You have new " + system + " requests to Approve");
    message.payload = {'messageFrom': uid};

    var options = { "gateway": "gateway.sandbox.push.apple.com", "cert": __dirname + "/../resource/cert.pem", "key:": __dirname + "/../resource/key.pem", "passphrase:": "secret" };
    var service = new apn.connection(options);

    service.on('connected', function() {
        console.log("Connected");
    });

    service.on('transmitted', function(notification, device) {
        console.log("Notification transmitted to:" + device.token.toString('hex'));
    });

    service.on('transmissionError', function(errCode, notification, device) {
        console.error("Notification caused error: " + errCode + " for device ", device, notification);
    });

    service.on('timeout', function () {
        console.log("Connection Timeout");
    });

    service.on('disconnected', function() {
        console.log("Disconnected from APNS");
    });

    service.on('socketError', console.error);

    var deviceId = "fe71811122723919abc56e7f8a8d8d8s8888c8s8a8s8s881155csafe";
    var device = new apn.Device(deviceId); 
    service.pushNotification(message, device);

1 个答案:

答案 0 :(得分:0)

如果我没有将冒号放入键和密码的密钥中,这可能会有所帮助。漫长的夜晚会对你这么做。 :/