2016-11-09 465 views
8

PHP蘋果改變了APN的驗證關鍵P8後到AP,目前的庫如https://github.com/immobiliare/ApnsPHP仍然使用舊的PEM和證書文件如何連接使用P8身份驗證密鑰文件

$push = new ApnsPHP_Push(
    ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, 
    'server_certificates_bundle_sandbox.pem' 
); 
// Set the Provider Certificate passphrase 
// $push->setProviderCertificatePassphrase('test'); 
// Set the Root Certificate Autority to verify the Apple remote peer 
$push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); 
// Connect to the Apple Push Notification Service 
$push->connect() 

連接與節點。 JS例子(https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift-3/),我可以把這樣的:

var apnProvider = new apn.Provider({ 
    token: { 
     key: 'APNsAuthKey_Q34DLF6Z6J.p8', // Path to the key p8 file 
     keyId: 'Q34DLF6Z6J', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key) 
     teamId: 'RLAHF6FL89', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/) 
    }, 
    production: false // Set to true if sending a notification to a production iOS app 
}); 

如何使用PHP遠程通知給iOS就像我在node.js中做什麼?

+0

您有解決方案嗎? :) – Maximus1809

+0

@ Maximus1809尚未:) – mehmetsen80

+0

我正在尋找一種方法來做到這一點,看起來像我將不得不作出解決 –

回答

-1

對不起,這麼晚了遊戲。如果我正確理解你的問題,我相信這就是你想要的。這就是我用PHP將消息發送給Apple APNS的原因。您可能需要對有效負載進行一些研究,因爲根據您編寫應用程序的方式,有幾種方法來構建它。另外,請記住,您必須能夠使用端口2195才能正常工作。如果你運行一個專用或內部服務器,你應該沒問題。如果它的共享服務器不起作用。

$passphrase = 'xxxxx'; // This is the passphrase used for file ck.pem 
    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); // ck.pem file must be included to sent token to ios devices 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
    stream_context_set_option($ctx, 'ssl', 'verify_peer', true); 
    stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true); 
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); 
    stream_set_blocking ($fp, 0); // Ensure that blocking is disabled 

    if (!$fp) { 
     $fds = "Failed to connect: $err $errstr" . PHP_EOL; 
     return false; 
    } else { 

     // Create the payload body 
     // this example uses a custom data payload. depending on your app you may need to change this 
     $body['aps'] = array('alert' => $message, 'sound' => 'default', 'badge' => 1); 
     // Encode the payload as JSON 
     $payload = json_encode($body); 
     // Build the binary notification 
     $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '',$token)) . pack('n', strlen($payload)) . $payload; 
     // Send it to the server 
     $result = fwrite($fp, $msg, strlen($msg)); 
     // Close the connection to the server 
     fclose($fp); 
    } 
相關問題