[英]how to send push notification in ios from server using php in newer version of ios, without using pem file
我已经在旧版本的 ios 中发送了通知。 但在较新的版本中,我无法创建 .pem 文件。 有人告诉我 pem 文件不再需要从服务器发送通知。 但不幸的是,我找不到任何关于此的链接。 有人请指导我如何在较新版本的 ios 中从服务器发送推送通知。 自上周以来,我一直在发送通知。 请帮忙。 这是我正在使用的代码
private function pushnotification($deviceToken, $message, $type, $badge, $userid, $jobid) {
$passphrase = '123456';
$ctx = stream_context_create();
// $file = base_path(). "/public/WenderCastPush.pem";
//stream_context_set_option($ctx, 'ssl', 'local_cert', $file);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true);
//stream_context_set_option($ctx, 'ssl','ciphers', 'TLSv1');
// Open a connection to the APNS server
// $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
$body['aps'] = array(
//'badge' => +1,
'alert' => $message,
'sound' => 'default',
//'title' => $message,
'type' => $type,
'userid' => $userid ,
'jobid' => $jobid,
);
// Encode the payload as JSON
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
$responce = 'Message not delivered' . PHP_EOL;
else
$responce = 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
return $responce;
}
您可以使用带有最新 PHP 版本的 iOS 发送通知,因为如果您的 PHP 代码不是最新版本,则通知发送将不起作用。
if(defined('CURL_HTTP_VERSION_2_0'))
{
$device_token = 'enter your device token';
$pem_file = 'enter your pem file path ;
$pem_secret = enter password';
$apns_topic = 'enter topic';
$sample_alert = '{"aps":{"alert":"hi yourporject name","sound":"default"}}';
$url = "https://api.development.push.apple.com/3/device/$device_token";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($response);
var_dump($httpcode);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.