簡體   English   中英

從服務器發送到APN的方式和內容,以便APN將通知發送到設備?

[英]How and What to send to APN from server so that APN send the notification to device?

我是一名iOS開發人員,有人問我要鍵入什么類型的請求(即標題和正文)才能擊中APN以向設備發送通知。

我閱讀了許多有關為APN設置服務器的教程,但由於我不了解PhP和Node J,所以無法理解。 讀完蘋果文件后,我知道它使用了http / 2和其他各種標記和值。 但是我無法提出完整的要求。 非常感謝您的幫助。

要使用PHP發送APNs請求,您需要滿足以下要求:

  1. .pem證書應該存在於您的php腳本的相同路徑中。
  2. 設備令牌,用於將通知發送到特定設備。

然后,您可以嘗試以下代碼:

<?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向APNS發出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證書。

https://github.com/noodlewerk/NWPusher

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM