Php 如何以及如何从服务器发送到APN,以便APN将通知发送到设备?

Php 如何以及如何从服务器发送到APN,以便APN将通知发送到设备?,php,ios,apple-push-notifications,siebel,apn,Php,Ios,Apple Push Notifications,Siebel,Apn,我是一名iOS开发人员,有人问我要点击APN以获取设备通知的请求类型(即整个头部和主体) 我读了很多关于APN服务器设置的教程,但我无法理解,因为我不了解PhP和Node Js。在阅读了苹果的文档后,我知道它使用了http/2和其他各种标记和值。但我无法构造完整的请求。 非常感谢您的帮助 要使用PHP发送APNs请求,需要满足以下要求: 一个.pem证书,应该存在于php脚本的同一路径中 设备令牌,用于向特定设备发送通知 然后,您可以尝试以下代码: <?php $apnsServe

我是一名iOS开发人员,有人问我要点击APN以获取设备通知的请求类型(即整个头部和主体)

我读了很多关于APN服务器设置的教程,但我无法理解,因为我不了解PhP和Node Js。在阅读了苹果的文档后,我知道它使用了http/2和其他各种标记和值。但我无法构造完整的请求。
非常感谢您的帮助

要使用PHP发送APNs请求,需要满足以下要求:

  • 一个
    .pem
    证书,应该存在于php脚本的同一路径中
  • 设备令牌,用于向特定设备发送通知
  • 然后,您可以尝试以下代码:

    <?php
        $apnsServer = 'ssl://gateway.push.apple.com:2195';
        $privateKeyPassword = '1234'; // your .pem private key password
    
        $message = 'Hello world!';
    
        $deviceToken = 'YOUR_DEVICE_TOKEN_HERE';
    
        $pushCertAndKeyPemFile = 'PushCertificateAndKey.pem'; // Your .pem certificate
        $stream = stream_context_create();
        stream_context_set_option($stream,
        'ssl',
        'passphrase',
        $privateKeyPassword);
        stream_context_set_option($stream,
        'ssl',
        'local_cert',
        $pushCertAndKeyPemFile);
    
        $connectionTimeout = 20;
        $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
        $connection = stream_socket_client($apnsServer,
        $errorNumber,
        $errorString,
        $connectionTimeout,
        $connectionType,
        $stream);
        if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        exit;
        } else {
        echo "Successfully connected to the APNS...";
        }
        $messageBody['aps'] = array('alert' => $message,
        'sound' => 'default',
        'badge' => 2,
        );
        $payload = json_encode($messageBody);
        $notification = chr(0) .
        pack('n', 32) .
        pack('H*', $deviceToken) .
        pack('n', strlen($payload)) .
        $payload;
        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
        if (!$wroteSuccessfully){
        echo "Could not send the message.";
        }
        else {
        echo "Successfully sent the message.";
        }
        fclose($connection);
    
    ?>
    
    
    

    有关更多详细信息,请参阅此部分。

    我们只使用普通CURL对APN执行http2请求

    先决条件:您拥有从开发者控制台转换为.PEM的有效SSL证书

    /usr/local/Cellar/curl/7.50.0/bin/curl -v \
    -d '{"aps":{"alert":"Hello","content-available": 1, "sound": ""}}' \
    -H "apns-topic: com.yourapp.bundleid" \
    -H "apns-expiration: 1" \
    -H "apns-priority: 10" \
    --http2 \
    --cert /Users/PATHTOPEM/key.pem:YOURPASSWORD \
    https://api.push.apple.com/3/device/YOURDEVICETOKEN
    
    或者,如果您对使用终端持谨慎态度,请尝试使用此MacOS应用程序发送推送通知,这非常简单

    先决条件:您需要在密钥链中具有证书签名权限和专用SSL证书。


    恐怕这行不通,您需要制作一个带有正确头键和有效JSON正文以及.pem文件的POST API。