繁体   English   中英

向多个设备发送推送通知:一段时间后,APNS响应为否定

[英]Send Push Notification to Multiple Devices: APNS response is negative after a while

我正在尝试发送多个设备的通知。 因此,我获得了指向数组的令牌,打开连接,循环发送通知,关闭连接。

但是,在9-10个设备之后,它将停止发送。 我相信Apple会以某种方式断开连接。

这是我的代码:

$message = 'Push';
$passphrase = 'mypass';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'MyPemFile.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.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 Apple service. ' . PHP_EOL;

// Encode the payload as JSON
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );
$payload = json_encode($body);

$result = 'Start'.PHP_EOL;
$tokenArray = array('mytoken');
foreach ($tokenArray as $item)
{
// Build the binary notification
$msg = chr(0).pack('n', 32).pack('H*', $item).pack('n', strlen($payload)).$payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Failed message'.PHP_EOL;
else
    echo 'Successful message'.PHP_EOL;
}
// Close the connection to the server
fclose($fp);

我的代码有问题吗? 我想我需要打开一次连接,发送通知然后关闭。 我应该使用多个令牌执行fwrite()吗? 我不知道如何。 任何想法或解决方案都可以接受。

顺便说一句,答案是这样的:

Successful message  
Successful message  
Successful message  
Successful message  
Successful message  
Successful message  
Successful message  
Successful message  
Successful message  
Successful message
Failed message
Failed message
Failed message
Failed message
Failed message
...
Failed message

PS我有相同代码的字符问题,但是在另一个问题中解决了,这是另一个问题,而不是重复。

第一条失败的消息可能与它有问题,这时Apple将关闭连接以向您发出信号,表明存在问题。 如果您使用的是增强格式,则您有机会在Apple关闭连接之前获得一些反馈,以查看发送的通知出了什么问题。 发生这种情况后,您必须重新建立连接以发送更多消息。

有多种原因可能导致它失败。 您可能发送了无效的设备令牌,有效负载可能无效或长度错误等。

最好查看APNS的文档: http : //developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM