如何以及从服务器向APN发送什么以便APN将通知发送给设备?

问题描述 投票:0回答:2

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

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

php ios apple-push-notifications siebel apn
2个回答
0
投票

要使用PHP发送APN请求,您需要以下要求:

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

?>

有关更多详细信息,请参阅此link


0
投票

我们只使用普通的CURL来向APNS发出http2请求

先决条件:您有一个有效的SSL证书从开发人员控制台转换为.PEM

/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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.