我对通知推送和iOs 10(使用Swift 3)有一点问题..
我遵循了本教程:https://www.sitepoint.com/developing-push-notifications-for-ios-10/ 使用 Pusher ,没关系,工作正常.. 我在手机上收到了通知。
但我想从我的网络服务器发送通知.. 我生成了我的.pem文件(http://www.apptuitions.com/generate-pem-file-for-push-notification/)
我的PHP代码:
$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/>";
}
结果: 已成功连接到APNS。处理... 已成功发送消息 但我从未收到手机通知
为什么? 如何使用php发送通知推送?
感谢您的帮助:)
[编辑] 好的,我找到了解决方案.. 最后添加:
fclose($connection);