【问题标题】:How to connect to apn with PHP using p8 auth key file如何使用 p8 身份验证密钥文件通过 PHP 连接到 apn
【发布时间】:2017-03-22 21:02:45
【问题描述】:

Apple 将 APNs Auth Key 更改为 p8 后,https://github.com/immobiliare/ApnsPHP 等当前库仍然使用旧的 pem 和 cert 文件连接

$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()

使用 Node.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
});

如何像在 node.js 中一样使用 PHP 向 iOS 发送远程通知?

【问题讨论】:

标签: php ios apple-push-notifications apn apns-php


【解决方案1】:

也许为时已晚,但我一直在努力解决这个问题,因为几乎每条指令都是针对旧方法的。

所以我决定用我的解决方案来回答这个“老”问题。

private static function sendNotificationAPN($device_token, $title, $body, $category, $sound, $optionals) {
        $alert = array(
            'aps' => array(
                'alert'    => array(
                    'title' => $title,
                    'body'  => $body
                ),
                'badge' => 0,
                'sound'    => $sound,
                'category' => $category,
                'content-available' => true
            )
        );

        foreach ($optionals as $key => $option) {
            $alert[$key] = $option;
        }

        $alert = json_encode($alert);

        $url = 'https://api.development.push.apple.com/3/device/' . $device_token['apn'];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $alert);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: '/* your app name */'"));
        curl_setopt($ch, CURLOPT_SSLCERT, /* absolute path to cert file */);
        curl_setopt($ch, CURLOPT_SSLCERTPASSWD, /* passphrase for cert */);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        $response = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


        $ret = array(
            'body' => $response,
            'httpcode' => $httpcode
        );

        return $ret;
    }

【讨论】:

    【解决方案2】:

    很抱歉这么晚才玩游戏。如果我正确理解您的问题,我相信这就是您要寻找的。这是我用来使用 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);
        }
    

    【讨论】:

    • 请使用新的JWT认证。
    猜你喜欢
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多