2014-02-25 92 views
-1

我有一个PHP脚本,它的伟大工程,在APNS将消息发送到我的iPhone,但是当我尝试使用节点APN模块,我得到错误513节点APN错误513尝试发送推送通知

在我的PHP代码中,我做的事情有点不同(见下文);即,我从设备令牌中删除空格,并将其删除。我还使用Apple推荐使用openssl cli创建的组合cert-key.pem。在node-apn中,似乎必须分别定义证书和密钥,因此我只使用从openssl创建的相应ce​​rt/key 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); 

回答

0

它可能会帮助,如果我没有把冒号在关键和密码的关键。漫长的夜晚会为你做到这一点。 :/