My App is using APNS push. Now I read that you can leave the connection open while sending multiple messages. So reason enough for me to change some scripts.
My APNS class has the following methods(and many more, but these are the most important for now):
public function setRegisterId($registerId){
$this->registerId = $registerId;
}
public function setupConnection()
{
$APNSdetails[1] = array('pemFile'=>'test.pem', 'passPhrase'=>'passPhrase');
$APNSdetails[2] = array('pemFile'=>'mag.pem', 'passPhrase'=>'passPhrase');
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', '/as/'.$APNSdetails[$this->appId]['pemFile']);
stream_context_set_option($ctx, 'ssl', 'passphrase', $APNSdetails[$this->appId]['passPhrase']);
// Open a connection to the APNS server
$this->connection = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$this->connection){
return FALSE;
}
else{
return TRUE;
}
}
public function sendMessage()
{
$RegisterIds = $this->registerId;
foreach ($RegisterIds AS $key => $value){
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $RegisterIds[$key]['registerId']) . pack('n', strlen($this->payload)) . $this->payload;
// Send it to the server
$result = fwrite($this->connection, $msg, strlen($msg));
$RegisterIds[$key]['payload'] = $this->payload;
if (!$result){
$RegisterIds[$key]['result'] = 'Message not delivered';
}
else{
$RegisterIds[$key]['result'] = 'Message successfully delivered';
}
}
$this->results = $RegisterIds;
}
Now I'm testing the script with some user typed data: (i've removed last 6 characters of registerId, for safety purposes). Using this array, both devices are receiving the Push Message.
Array
(
[0] => Array
(
[registerId] => ddbb8b9b27beed70e56866d07a3da5f4cabf71ffab4606967a523e93eb
[id] => 214
[deviceOs] => apple
)
[1] => Array
(
[registerId] => c20f10e0b7f345988440d860bfb179f5b95a47d7eee714f5e0268c2c93
[id] => 234
[deviceOs] => apple
)
)
Using a query to retrieve other (more real live) data provided with the following array:
Array
(
[0] => Array
(
[registerId] => 165ad6daa6e6e2c470d1c2777901e731f34fbe419f7d93cda383665c39
[id] => 1111
[deviceOS] => apple
)
[1] => Array
(
[registerId] => ddbb8b9b27beed70e56866d07a3da5f4cabf71ffab4606967a523e93eb
[id] => 1328
[deviceOS] => apple
)
[2] => Array
(
[registerId] => 7383d29d4bc210b32ea409e46a99ca522a3cf4a51e330f7d7457f7bc88
[id] => 359
[deviceOS] => apple
)
[3] => Array
(
[registerId] => c20f10e0b7f345988440d860bfb179f5b95a47d7eee714f5e0268c2c93
[id] => 1148
[deviceOS] => apple
)
)
As you can see that are 2 the same registerId's and 2 new registerId's. However, none of the 4 devices is receiving a push notification.
I'm, kind of stuck or lost... Why is the second array failing?