Last active
October 26, 2016 15:14
-
-
Save finagin/7ad8649578ea7d4dd98778052ae5549f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function send_push_ios( $message, $data = array() ) { | |
// Это паскод от сертификата | |
$passphrase = "Gergemona123"; | |
// Ссылка на файл сертификата на сервере | |
$sert_file = __DIR__ . '/gto_client.pem'; | |
// Ссылка на сервер Apple | |
// песочница | |
$push_url = 'ssl://gateway.sandbox.push.apple.com:2195'; | |
// продакшн | |
//$push_url = 'ssl://gateway.push.apple.com:2195'; | |
// Токен | |
$token = '436cc258378f0c7ec734fd5c5809dc11f36a2ecd0d071677f68c2e0b8968ddd5'; | |
// Создание стрима | |
$ctx = stream_context_create(); | |
stream_context_set_option( $ctx, 'ssl', 'local_cert', $sert_file ); | |
stream_context_set_option( $ctx, 'ssl', 'passphrase', $passphrase ); | |
$fp = stream_socket_client( $push_url, $err, $err_str, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx ); | |
if ( ! $fp ) { | |
return array( 'success' => FALSE, 'error' => $err, 'error2' => $err_str ); | |
} | |
$body[ 'aps' ] = array( 'alert' => $message, 'sound' => 'default' ); | |
if ( $data ) { | |
$body[ 'aps' ][ 'data' ] = $data; | |
} | |
$payload = json_encode( $body ); | |
$msg = chr( 0 ) . pack( 'n', 32 ) . pack( 'H*', $token ) . pack( 'n', strlen( $payload ) ) . $payload; | |
$msg = chr( 1 ) . pack( 'n', 32 ) . pack( 'H*', $token ) . chr( 2 ) . pack( 'n', strlen( $payload ) ) . $payload; | |
stream_set_blocking( $fp, 0 ); | |
$result = fwrite( $fp, $msg, strlen( $msg ) ); | |
fclose( $fp ); | |
if ( ! $result ) { | |
return array( 'success' => FALSE, 'error' => 'Message not delivered' ); | |
} else { | |
return array( 'success' => TRUE ); | |
} | |
} | |
function send_push_android( $head, $title, $message, $data = array() ) { | |
$token = ''; | |
define( 'API_ACCESS_KEY', 'AIzSB-J4l4xF-L-MSO5BK5ER' ); | |
$msg = array( | |
'message' => $message, //текст под заголовком при открытии | |
'title' => $head, // наверху надпись | |
'subtitle' => $title, //заголовок при открытии | |
'tickerText' => '', | |
'vibrate' => 1, | |
'sound' => 1 | |
); | |
if($data) { | |
$msg['data'] = $data; | |
} | |
$fields = array( | |
'registration_ids' => array($token), | |
'data' => $msg | |
); | |
$headers = array( | |
'Authorization: key=' . API_ACCESS_KEY, | |
'Content-Type: application/json' | |
); | |
$ch = curl_init(); | |
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' ); | |
curl_setopt( $ch,CURLOPT_POST, true ); | |
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers ); | |
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); | |
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) ); | |
$result = curl_exec($ch); | |
curl_close( $ch ); | |
return $result; | |
} | |
// Вызов | |
//send_push_ios( 'Test message' ) | |
print_r( | |
send_push_ios( | |
'Test message', | |
array( | |
'type' => 'type', | |
'name' => 'name', | |
'standartId' => 'standartId', | |
'medal' => 'medal', | |
) | |
) | |
); | |
/* | |
https://gist.github.com/joashp/b2f6c7e24127f2798eb2 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment