I have a little problem with notification push and iOs 10 (with Swift 3)..
I followed this tutorial : https://www.sitepoint.com/developing-push-notifications-for-ios-10/ With Pusher, it's ok, works fine.. I receive the notification on my phone.
But I would like to send notifications from my web server.. I generate my .pem file (http://www.apptuitions.com/generate-pem-file-for-push-notification/)
My php code :
$apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';
$privateKeyPassword = 'mykey';
$message = "My message here !";
$deviceToken ='MYTOKENHERE';
$pushCertAndKeyPemFile = 'pushcert.pem';
$stream = stream_context_create();
stream_context_set_option($stream,'ssl','passphrase',$privateKeyPassword);
stream_context_set_option($stream,'ssl','local_cert',$pushCertAndKeyPemFile);
$connectionTimeout = 30;
$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection = stream_socket_client($apnsServer, $errorNumber, $errorString, $connectionTimeout,$connectionType,$stream);
if (!$connection){
echo "Failed to connect to the APNS server. Error = $errorString <br/>";
exit;
}
else{
echo "Successfully connected to the APNS. Processing...</br>";
}
$messageBody['aps'] = array('alert' => $message,'badge' => 1, 'sound' => 'default');
$payload = json_encode($messageBody);
$notification = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
if (!$wroteSuccessfully){
echo "Could not send the message<br/>";
} else {
echo "Successfully sent the message<br/>";
}
Result : Successfully connected to the APNS. Processing... Successfully sent the message But I never receive notification on my phone
Why ? How to send a notification push with php ?
Thanks for your help :)
[EDIT] Ok, i found a solution.. Just add at the end:
fclose($connection);