发送多个iPhone的通知 [英] Sending multiple iPhone notifications

查看:221
本文介绍了发送多个iPhone的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code工程确定,当我需要发送一个通知,但每次当我需要发送多个时间,它只发送第一个。这里是code:

My code works ok when I need to send one notification, but each time when I need to send more than one, it only sends the first one. Here is the code:

<?php
$device_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);

$payload['aps'] = array('alert' => 'some notification', 'badge' => 0, 'sound' => 'none');
$payload = json_encode($payload);

for($i=0; $i<5; $i++)
{
	$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $device_token)) . chr(0) . chr(strlen($payload)) . $payload;

	fwrite($apns, $apnsMessage);
}?>

我在做什么错了?

What am I doing wrong?

Thx提前,
Mladjo

Thx in advance, Mladjo

推荐答案

您应该打开到APNS连接只有一次。现在你是在循环这是不对的打开它。我还使用一个稍微不同的方案来建立我的消息。您应该做的是这样的:

You should open the connection to apns only once. Right now you are opening it in the loop which is wrong. I'm also using a slightly different scheme to build my messages. You should instead do it in this way:

$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
for($i=0; $i<5; $i++)
{
        $apns_message = chr(0).pack('n', 32).pack('H*', $device_token).pack('n', strlen($payload)).$payload;

        fwrite($apns, $apnsMessage);
}?>

另外请注意,苹果建议使用相同的连接来发送所有推送通知,请在每一次有一个推送通知发送时间无法连接。

Also note that apple recommends using the same connection to send all your push notifications so you shouldn't connect every time you have a push notification to send.

这篇关于发送多个iPhone的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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