Отправка PUSH уведомлений в приложение IOS
Задача: в приложение на IOS отправить push уведомление из PHP
Решение:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
public function SendPushIOS($token,$title,$message){ $rs=false; $notification_payload = [ "aps" => [ "alert" => [ //"title" => "Зарядись!", "body" => $message, // "action-loc-key" => "PLAY" ], "badge" => 0, "sound" => "bingbong.aiff" ] ]; $token_key = $this->server_key_ios; //echo "!! token_id IOS: $this->server_token_id_ios\n"; $jwt_header = [ 'alg' => 'ES256', 'kid' => $this->server_token_id_ios ]; //echo "!!team_id:$this->server_team_id_ios\n"; $jwt_payload = [ 'iss' => $this->server_team_id_ios, 'iat' => time() ]; $raw_token_data = self::b64($jwt_header, true).".".self::b64($jwt_payload, true); $signature = ''; openssl_sign($raw_token_data, $signature, $token_key, 'SHA256'); $jwt = $raw_token_data.".".self::b64($signature); // send push $request_body = json_encode($notification_payload); $endpoint = 'https://api.push.apple.com/3/device'; $url = "$endpoint/$token"; $ch = curl_init($url); //echo "!!server_bandle_id_ios:$this->server_bandle_id_ios\n"; curl_setopt_array($ch, [ CURLOPT_POSTFIELDS => $request_body, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, CURLOPT_HTTPHEADER => [ "content-type: application/json", "authorization: bearer $jwt", "apns-topic: $this->server_bandle_id_ios" ] ]); $result_curl=curl_exec($ch); $result = json_decode($result_curl); if (is_null($result)) { $log= new TLog($this->api); $log->InsertLogBoiler([ "source_id"=> TLog::S_Messages, "comment"=>"Ошибка отправки PUSH IOS", "raw_package"=>curl_error($ch).$result_curl, "reason"=> TLog::R_Error ]); } else { if ((int)$result>0){ //if ($result->success>0){ $rs=true; }; }; curl_close($ch); return $rs; } public function b64($raw, $json=false){ if($json) $raw = json_encode($raw); return str_replace('=', '', strtr(base64_encode($raw), '+/', '-_')); } |