如何使用苹果推送通知 p12 证书文件通过代码设置 ios 平台?

How to setup ios platform through code with apple push notification p12 certificate file?

我想用 ios 从 .p12 文件设置的平台创建一个应用程序。我该怎么做?

这是创建应用程序的方法:

class AppHandler
{
    public $USER_AUTH_KEY = 'Insert your key here';

    public function create($name, $apns_p12 = null, $apns_p12_password = null, $gcm_key = null, $android_gcm_sender_id = null)
    {
        $fields = array(
            'name' => $name,
            'apns_p12' => $apns_p12,
            'apns_p12_password' => $apns_p12_password,
            'gcm_key' => $gcm_key,
            'android_gcm_sender_id' => $android_gcm_sender_id
        );

        $fields = json_encode($fields);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/apps");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
            "Authorization: Basic " . $this->USER_AUTH_KEY));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        try {
            $response = curl_exec($ch);

            if (!$response) {
                throw new Exception("App wasn't created");
            }
        } catch (Exception $e) {
            echo 'Error: ', $e->getMessage(), "\n";
        } finally {
            curl_close($ch);
        }

        $response = json_decode($response, true);

        $return = array(
            'id' => $response['id'],
            'basic_auth_key' => $response['basic_auth_key']
        );

        return $return;
    }
...

这是通过两种方式获取 .p12 文件内部的方法:

    public function getP12($pkcs12, $password = NULL): string
    {

        /*
        // Way 1:
        $pkcs12 = file_get_contents($pkcs12);
        $encoded = base64_encode($pkcs12);

        return $encoded;
        */

        // Way 2:
        $cert_store = file_get_contents($pkcs12);
        if (!$cert_store) {
            echo "Error: can't read file.\n";
            exit;
        }

        $pkcs12Read = openssl_pkcs12_read($cert_store, $cert_info, $password);
        if ($pkcs12Read) {
            $result = base64_encode($cert_info['cert']);
            return $result;
        } else {
            echo "Error: can't read cert.\n";
            exit;
        }
    }

根据 onesignal's doc 我必须发送 apns_p12 作为 我的苹果推送通知 p12 证书文件,转换为字符串和 Base64 编码.

我是这样做的:

$obj = new AppHandler();
$response = $obj->create('TestName', $obj->getP12('cert.p12', 'password'), 'password')

它创建了一个给定名称的应用程序,但是,平台没有设置。

"the platform is not set up" 是什么意思?您收到什么错误以及在哪里?

顺便说一下,我最终放弃了尝试编写复杂的 APNS 编程代码,而是使用 AWS 的简单通知服务:https://aws.amazon.com/sns。它通过使用 API 设置主题和订阅者来处理 Apple 和 Google 通知,此外,您每月最多可以免费发送 100 万条通知。

好的,我知道了。我只需要添加 apns_env 参数:

$fields = array(
            'name' => $name,
            'apns_env' => $apns_env,
            'apns_p12' => $apns_p12,
            'apns_p12_password' => $apns_p12_password,
            'gcm_key' => $gcm_key,
            'android_gcm_sender_id' => $android_gcm_sender_id
        );

而且我应该了解文件的内部结构并将它们转换为字符串并采用 Base64 编码:

public function getP12($pkcs12): string
    {
        $apns_12 = base64_encode(file_get_contents($pkcs12));

        return $apns_12;
    }