php - 如何使用 p8 授权 key 文件通过 PHP 连接到 apn

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

苹果将APNs Auth Key改为p8后,目前的库如https://github.com/immobiliare/ApnsPHP仍然使用旧的 pem 和证书文件进行连接

$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 发送远程通知?

最佳答案

也许为时已晚,但我为此苦苦挣扎,因为几乎每条指令都是针对旧方法的。

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

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;
    }

关于php - 如何使用 p8 授权 key 文件通过 PHP 连接到 apn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500423/

相关文章:

php - 当前文档的 PHP 中的最后更新日期

php - 如何在没有键的情况下将 JSON 数据转换为数组?

IOS/objective-C : Shared Instance Method with argument

ios - 居中的 UITextField 文本在编辑时意外移动

ios - 是否可以通过远程通知的方式打开iOS应用?

php - 带有数据库 (mysql) 查询的 WordPress 插件

php - 将整个日志文件保存在一个变量中?

ios - 测试目标 X 遇到错误(早期意外退出,操作从未完成引导 - 不会尝试重新启动

ios - 使用企业分发在 iOS 8 和 iOS 9 上推送通知

ios - 仅在发布版本中找不到适用于应用程序的有效 'aps-environment' 权利字符串