Why not push information through apple's apns? PhpclassphpApns {*** @ varstring certificate address * protected $ cert_path; *** @ varstring * protected $ password; *** @ varbool is the sandbox * protected $ is_sandbox; * ** @ varstring official push server *
Why not push information through apple's apns? Phpclass phpApns {/*** @ var string certificate address */protected $ cert_path;/*** @ var string password */protected $ password; /*** @ var bool whether it is a sandbox */protected $ is_sandbox;/*** @ var string officially pushes the server */
Push information through apple's apns <无>
Cert_path = $ cert_path; $ this-> password = $ password; $ this-> is_sandbox = $ is_sandbox ;} /*** @ param $ device_token * @ param $ msg: the message sent by the device_token device * @ param string $ sound push sound */public function pushMessage ($ device_token, $ msg, $ sound = 'default') {$ apns = stream_context_create (); stream_context_set_option ($ apns, 'ssl ', 'local _ cert', $ this-> cert_path ); stream_context_set_option ($ apns, 'ssl ', 'passp Hrase ', $ this-> password); if ($ this-> is_sandbox) {$ uri = $ this-> sandbox_uri ;} else {$ uri = $ this-> publish_uri;} // set 60s timeout $ fp = stream_socket_client ($ uri, $ err, $ errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ apns); if (! $ Fp) {$ errMsg = "connection opening failed: ($ err) $ errstr"; die ($ errMsg );} // composition of the information stream sent to apns $ body ['aps '] = array ('alert' => $ msg, 'sound' => $ sound ); $ json = json_encode ($ body); // convert it to a binary stream $ message = chr (0 ). pack ('n', 32 ). pack ('H * ', $ device_token ). pack ('n', strlen ($ json )). $ json; $ result = fwrite ($ fp, $ message, strlen ($ message); if (! $ Result) {die ('information push failed! ');} Fclose ($ fp); echo' push successful! ';}}