Push Notification on IOS by

30
Jun
0

วันนี้เราจะมานำเสนอเกี่ยวกับการส่ง 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!

ไม่มีความเห็น

ยังไม่มีความเห็น

ใส่ความเห็น

RSS feed for comments on this post

 เราชนะรอบ 4 | ยืมเงิน 3000 ด่วน | แอพกู้เงิน | แอพเงินด่วน | สินเชื่อออนไลน์อนุมัติทันที | Site Map | กู้เงินก้อน | กระเป๋าตัง | thisshop และ ยืมเงินฉุกเฉิน 5000 ด่วน