Phpios push (code)

Source: Internet
Author: User
This article provides a detailed analysis of phpios push. For more information, see The code is as follows:
// Ssl (OpenSSL) support must be enabled for php.
$ ApnsCert = "ck. pem"; // certificate license file used to connect to APNS. the certificate must be created as required.
$ Pass = "123456"; // certificate password
$ ServerUrl = "ssl: // gateway.sandbox.push.apple.com: 2195"; // push server, which is a development test server.
$ DeviceToken = "courier"; // ios device id. there cannot be spaces in the middle. Each ios device has one id.
$ Message = $ _ GET ['message'] or $ message = "hello! ";
$ Badge = (int) $ _ GET ['badge'] or $ badge = 2;
$ Sound = $ _ GET ['sound'] or $ sound = "default ";
$ Body = array ('aps '=> array ('alert' => $ message, 'badge' => $ badge, 'sound' => $ sound ));
$ StreamContext = stream_context_create ();
Stream_context_set_option ($ streamContext, 'SSL ', 'local _ cert', $ apnsCert );
Stream_context_set_option ($ streamContext, 'SSL ', 'passphrase', $ pass );
$ Apns = stream_socket_client ($ serverUrl, $ error, $ errorString, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ streamContext); // connect to the server
If ($ apns ){
Echo "Connection OK
";
} Else {
Echo "Failed to connect $ errorString ";
Return;
}
$ Payload = json_encode ($ body );
$ Msg = chr (0 ). pack ('N', 32 ). pack ('H * ', str_replace ('','', $ deviceToken )). pack ('N', strlen ($ payload )). $ payload;
$ Result = fwrite ($ apns, $ msg); // send a message
Fclose ($ apns );
If ($ result)
Echo "Sending message successfully:". $ payload;
Else
Echo 'message not delivered ';
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.