Push Notification on IOS by tosawat
Jun0
วันนี้เราจะมานำเสนอเกี่ยวกับการส่ง Push Notification ให้กับมือถือที่ใช้ระบบปฏิบัติการ IOS กันนะครับ
ก่อนอื่นมาดูขั้นตอนการทำงานของระบบ Push Notification กันก่อนนะครับ
เราจะมาพูดกันถึง Code ฝั่ง Server นะครับ ในที่นี้เราจะใช้ PHP นะครับ
$device_token = ’491956db82a4677a610d11e0574ed42efa686c0d3b5a7900b4eec810a2d42c97′;
$is_dev = true; // true or false
if($is_dev){
// Put your private key’s passphrase here:
$passphrase = ‘passpharse’;
$key_path = base_path().’/key/development.pem’;
$host = ‘ssl://gateway.sandbox.push.apple.com’;
$port = ’2195′;
}else{
// Put your private key’s passphrase here:
$passphrase = ‘passpharse’;
$key_path = base_path().’/key/production.pem’;
$host = ‘ssl://gateway.push.apple.com’;
$port = ’2195′;
}
$ctx = stream_context_create();
stream_context_set_option($ctx, ‘ssl’, ‘local_cert’, $key_path);
stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client($host.’:’.$port, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if($fp){
// Encode the payload as JSON
$payload = json_encode(array(
‘aps’ => array(
‘alert’ => ‘This is a book.’
),
‘data’ => array(1,2,3)
)); // เราสามารถส่งข้อมูลเพิ่มเติมไปได้พร้อมๆกับ Payload ครับ
// Build the binary notification
$binary = chr(0).pack(‘n’, 32).pack(‘H*’, $device_token).pack(‘n’, strlen($payload)).$payload;
// Send it to the server
fwrite($fp, $binary, strlen($binary));
}
// Close the connection to the server
@socket_close($fp);
@fclose($fp);
credit: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
Enjoy this article?
Consider subscribing to our RSS feed!
ไม่มีความเห็น
ยังไม่มีความเห็น