2017-01-13 1 views
3

Apple에서 푸시 알림 서비스를 업데이트했으며받은 인증서 파일이 이제 .p8 파일입니다. .pem 파일로 푸시 알림을 보내는 방법에 대한 온라인 예제는 많이 있지만 .p8 파일에 대해서는 아무 것도 찾을 수 없습니다. 누구든지 .p8 파일과 작동하는 코드가 있습니까?iOS 푸시 알림을 .p8 파일로 PHP로 전송

답변

0

PHP를 사용하여 새로운 JWT based push notification service 푸시 알림을 보내려고했습니다. 그것은 확실히 쉬운 일이 아니었다.

GitHub에 프로젝트를 업로드했습니다. 거기에서 다운로드하여 .p8 파일을 기존 .p8 파일로 바꿀 수 있습니다.

그런 다음 push.php 파일에 사용자가 교체 할 필요가 당신의 kid, iss(Team ID), tokenapp_bundle_id.

디렉토리로 이동하여 터미널에서 php push.php 명령을 실행하십시오. 모든 것이 잘되면 푸시 알림을 받아야합니다.

이 솔루션은 저에게 적합합니다. 터미널에서 오류가 발생하지 않도록하십시오.

이 정보가 도움이되기를 바랍니다.

+0

내가하려고하지만 PHP5에서 작동하지 않은 --with-nghttp2 및 OpenSSL을 플래그로 컴파일해야합니다 .6 버전 오류. –

1

아래 스크립트를 사용하면 .p8 파일과 함께 토큰 기반 푸시 알림을 보낼 수 있습니다. 이 지원 컬의

최소 버전은 7.38.0이며, 그것은> = 1.0.2

<?php 

    $keyfile = 'AuthKey_AABBCC1234.p8';    # <- Your AuthKey file 
    $keyid = 'AABBCC1234';       # <- Your Key ID 
    $teamid = 'AB12CD34EF';       # <- Your Team ID (see Developer Portal) 
    $bundleid = 'com.company.YourApp';    # <- Your Bundle ID 
    $url = 'https://api.development.push.apple.com'; # <- development url, or use http://api.push.apple.com for production environment 
    $token = 'e2c48ed32ef9b018........';    # <- Device Token 

    $message = '{"aps":{"alert":"Hi there!","sound":"default"}}'; 

    $key = openssl_pkey_get_private('file://'.$keyfile); 

    $header = ['alg'=>'ES256','kid'=>$keyid]; 
    $claims = ['iss'=>$teamid,'iat'=>time()]; 

    $header_encoded = base64($header); 
    $claims_encoded = base64($claims); 

    $signature = ''; 
    openssl_sign($header_encoded . '.' . $claims_encoded, $signature, $key, 'sha256'); 
    $jwt = $header_encoded . '.' . $claims_encoded . '.' . base64_encode($signature); 

    // only needed for PHP prior to 5.5.24 
    if (!defined('CURL_HTTP_VERSION_2_0')) { 
     define('CURL_HTTP_VERSION_2_0', 3); 
    } 

    $http2ch = curl_init(); 
    curl_setopt_array($http2ch, array(
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, 
    CURLOPT_URL => "$url/3/device/$token", 
    CURLOPT_PORT => 443, 
    CURLOPT_HTTPHEADER => array(
     "apns-topic: {$bundleid}", 
     "authorization: bearer $jwt" 
    ), 
    CURLOPT_POST => TRUE, 
    CURLOPT_POSTFIELDS => $message, 
    CURLOPT_RETURNTRANSFER => TRUE, 
    CURLOPT_TIMEOUT => 30, 
    CURLOPT_HEADER => 1 
)); 

    $result = curl_exec($http2ch); 
    if ($result === FALSE) { 
    throw new Exception("Curl failed: ".curl_error($http2ch)); 
    } 

    $status = curl_getinfo($http2ch, CURLINFO_HTTP_CODE); 
    echo $status; 

    function base64($data) { 
    return rtrim(strtr(base64_encode(json_encode($data)), '+/', '-_'), '='); 
    } 

?> 
+0

'예기치 않은 HTTP/1.x 요청 : POST/3/device/5cba9501b6b6b3d6xxxxxxxxxxxxx'와 같은 오류가 나타납니다. 해결책을 제안 할 수 있습니까? –

+0

이 답변에서 제안 된대로 웹 서버를 다시 시작해보십시오 : https://stackoverflow.com/a/48007808/1833495? –

관련 문제