Last active
December 22, 2015 14:08
-
-
Save urouro/6483237 to your computer and use it in GitHub Desktop.
Push通知送信
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendPushNotification() | |
{ | |
$deviceToken = '<64桁のデバイストークン>'; | |
// Push通知で表示するメッセージ | |
$alert = 'Push通知を送信しました'; | |
// Push通知受け取り時、アイコンに表示するバッジの数字 | |
$badge = 1; | |
// ペイロードの構成 | |
$body = array(); | |
$body['aps'] = array('alert' => $alert); | |
$body['aps']['badge'] = $badge; | |
// SSL証明書 | |
$cert = 'apns_dev.pem'; | |
// Push通知依頼先サーバ | |
$url = 'ssl://gateway.sandbox.push.apple.com:2195'; // Development | |
//$url = 'ssl://gateway.push.apple.com:2195'; // Production(AdhocかDistributionの場合) | |
// Push通知送信 | |
$context = stream_context_create(); | |
stream_context_set_option($context, 'ssl', 'local_cert', $cert); | |
$fp = stream_socket_client($url, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); | |
if(!$fp){ | |
print "接続失敗. $err $errstr \n"; | |
return; | |
} else { | |
print "送信完了. \n"; | |
} | |
$payload = json_encode($body); | |
$message = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload; | |
print "送信メッセージ :" . $payload . "\n"; | |
fwrite($fp, $message); | |
fclose($fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment