Home > php教程 > PHP源码 > ios消息推送

ios消息推送

PHP中文网
Release: 2016-05-25 17:05:50
Original
1002 people have browsed it

<?php
/*
include this content
define(&#39;DEV_APP_TOKEN&#39;,&#39;test token&#39;);
define(&#39;PRODUCT_APP_TOKEN&#39;,&#39;product token&#39;);
define(&#39;DEV_CERT_PASSPHRASE&#39;,&#39;DEV_CERT_PASSPHRASE&#39;);
define(&#39;PRODUCT_CERT_PASSPHRASE&#39;,&#39;PRODUCT_CERT_PASSPHRASE&#39;);
define(&#39;DEV_SSL_URL&#39;,&#39;ssl://gateway.sandbox.push.apple.com:2195&#39;);
define(&#39;PRODUCT_SSL_URL&#39;,&#39;ssl://gateway.push.apple.com:2195&#39;);
define(&#39;API_KEY&#39;,&#39;you key&#39;);

*/

include &#39;config.php&#39;;


$token = empty($_POST[&#39;dev_token&#39;]) ? &#39;&#39; : $_POST[&#39;dev_token&#39;]; //要推送的设备序列号
$message = empty($_POST[&#39;message&#39;]) ? &#39;&#39; : $_POST[&#39;message&#39;];//内容
$type = empty($_POST[&#39;type&#39;]) ? 0 : intval($_POST[&#39;type&#39;]);//消息类型
$url = empty($_POST[&#39;url&#39;]) ? &#39;&#39; : $_POST[&#39;url&#39;];//url
$badge = empty($_POST[&#39;badge&#39;]) ? 1 : intval($_POST[&#39;badge&#39;]); //未读消息数


if($token == &#39;&#39; || empty($token)){
	echo &#39;设备token不能为空&#39;;
	die;
}
// Put your device token here (without spaces):
$deviceToken = $token;

// Put your private key&#39;s passphrase here:
$passphrase = PRODUCT_CERT_PASSPHRASE;



////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;local_cert&#39;, &#39;ck.pem&#39;);
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;passphrase&#39;, $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    PRODUCT_SSL_URL, $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo &#39;Connected to APNS&#39; . PHP_EOL;



// Create the payload body
$body = array(&#39;aps&#39; => array(&#39;alert&#39; => $message,
						     &#39;sound&#39; => &#39;default&#39;,
						     &#39;type&#39; => $type,
						     &#39;url&#39; => $url,
                             &#39;badge&#39; => $badge
						      )
    ); 
    

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack(&#39;n&#39;, 32) . pack(&#39;H*&#39;, $deviceToken) . pack(&#39;n&#39;, strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo &#39;Message not delivered&#39; . PHP_EOL;
else
    echo &#39;Message successfully delivered&#39; . PHP_EOL;

// Close the connection to the server
fclose($fp);
    
?>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template