2019-02-13 05:02
浏览 115


I am an iOS developer and i was asked to what type(whole i.e header as well as body) of request to hit APN to get notification to device.

I read many tutorial for setup of server for APN but I am unable to understand as i have no knowledge about PhP and Node Js. After reading apple document , I came to know that it uses http/2 and other various tag and value. But i am unable to construct full request. Any help is highly appreciated.

图片转代码服务由CSDN问答提供 功能建议

我是iOS开发人员,我被问到请求的类型(整个,即标题和正文) 点击APN以获取设备通知。

我阅读了许多APN服务器设置教程但我无法理解,因为我对PhP和Node Js一无所知。 看完苹果文档后,我才知道它使用了http / 2和其他各种标签和值。 但我无法构建完整的请求。 非常感谢任何帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyuxin5353
    dongyuxin5353 2019-02-13 05:25

    To send an APNs request using PHP, you need these requirements:

    1. A .pem certificate which should exist in the same path of your php script.
    2. The Device Token, which is needed to send the notification to a specific device.

    Then you can try the following code:

        $apnsServer = 'ssl://';
        $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();
        $connectionTimeout = 20;
        $connection = stream_socket_client($apnsServer,
        if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        } 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)) .
        $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
        if (!$wroteSuccessfully){
        echo "Could not send the message.";
        else {
        echo "Successfully sent the message.";

    Refer to this link for more details.

    点赞 评论
  • doudao6529
    doudao6529 2019-02-13 05:55

    We just use plain CURL to do http2 requests to APNS

    Pre-requisites: You have a valid SSL cert converted to a .PEM from Developer console

    /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 \

    Or if you are wary of using the terminal try this MacOS app to send push notifications, it is so easy.

    Pre-requisites: You need to have the cert signing authority and the private SSL cert in your keychain.

    点赞 评论