我是一名iOS开发人员,有人问我要打出APN的哪种类型的请求(即标头和正文)来通知设备。
我阅读了许多有关为APN设置服务器的教程,但由于我不了解PhP和Node J,所以无法理解。读完苹果文件后,我知道它使用了http / 2和其他各种标记和值。但是我无法提出完整的要求。 非常感谢您的帮助。
答案 0 :(得分:0)
要使用PHP发送APNs请求,您需要满足以下要求:
.pem
证书,应该与您的php脚本位于同一路径。然后您可以尝试以下代码:
<?php
$apnsServer = 'ssl://gateway.push.apple.com:2195';
$privateKeyPassword = '1234'; // your .pem private key password
$message = 'Hello world!';
$deviceToken = 'YOUR_DEVICE_TOKEN_HERE';
$pushCertAndKeyPemFile = 'PushCertificateAndKey.pem'; // Your .pem certificate
$stream = stream_context_create();
stream_context_set_option($stream,
'ssl',
'passphrase',
$privateKeyPassword);
stream_context_set_option($stream,
'ssl',
'local_cert',
$pushCertAndKeyPemFile);
$connectionTimeout = 20;
$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 no = $errorNumber<br/>";
exit;
} else {
echo "Successfully connected to the APNS...";
}
$messageBody['aps'] = array('alert' => $message,
'sound' => 'default',
'badge' => 2,
);
$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.";
}
else {
echo "Successfully sent the message.";
}
fclose($connection);
?>
有关更多详细信息,请参考此link。
答案 1 :(得分:0)
我们只使用普通的CURL向APNS发出http2请求
先决条件:您具有从开发人员控制台转换为.PEM的有效SSL证书
/usr/local/Cellar/curl/7.50.0/bin/curl -v \
-d '{"aps":{"alert":"Hello","content-available": 1, "sound": ""}}' \
-H "apns-topic: com.yourapp.bundleid" \
-H "apns-expiration: 1" \
-H "apns-priority: 10" \
--http2 \
--cert /Users/PATHTOPEM/key.pem:YOURPASSWORD \
https://api.push.apple.com/3/device/YOURDEVICETOKEN
或者如果您担心使用终端,请尝试使用此MacOS应用发送推送通知,这很容易。
先决条件:您需要在钥匙串中具有证书签名授权和私有SSL证书。