Skip to content

Instantly share code, notes, and snippets.

@urouro urouro/gist:6483237
Last active Dec 22, 2015

Embed
What would you like to do?
Push通知送信
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
You can’t perform that action at this time.