新的 APNS 提供程序 API 和 PHP [英] New APNS Provider API and PHP

查看:18
本文介绍了新的 APNS 提供程序 API 和 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始创建一些基于 this 的代码,用于从 PHP 发送推送通知.>

但是,现在我知道有一个新的 API 使用 HTTP/2 并在响应中提供反馈,我正在尝试弄清楚我需要做什么才能获得该反馈.

我找不到任何教程或示例代码来指导我(我猜是因为它太新了).

是否可以使用 stream_socket_client() 方法通过新的提供程序 API 连接到 APNS?我如何获得反馈?我现在从 fwrite($fp, $msg, strlen($msg)) 返回的只是一个数字.出于所有意图和目的,您可以将我的代码视为与 SO question I based my code 中的代码相同在

谢谢!

解决方案

借助新的 HTTP/2 APNS 提供程序 API,您可以使用 curl 发送推送通知.

编辑

在继续之前(如@Madox 所述),应安装 openssl >= 1.0.2e(最好从软件包中).使用命令

验证

openssl 版本

a) 您的 PHP 版本应该 >= 5.5.24,以便定义常量 CURL_HTTP_VERSION_2_0.

b) 使用

确保您的系统中安装了 curl 7.46+ 版本

curl --version

c) Curl 应该启用 http/2 支持.在输入上一条命令时的输出中,您应该看到如下一行:

Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets

如果 HTTP2 没有出现,你可以按照这个优秀的教程安装 http/2 for curl https://serversforhackers.com/video/curl-with-http2-support

验证 curl 检测到 openssl >= 1.0.2e,执行 curl --version 应该输出如下内容:

curl 7.47.1 (x86_64-pc-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.2f zlib/1.2.8 libidn/1.28 nghttp2/1.8.0-DEV librtmp/2.3

e) 安装完所有内容后,您可以在命令行中对其进行测试:

curl -d '{"aps":{"alert":"hi","sound":"default"}}' \--cert <your-certificate.pem>:<certificate-password>\-H "apns-topic: <your-app-bundle-id>"\--http2\https://api.development.push.apple.com/3/device/

f) 这是我成功尝试过的 PHP 示例代码:

if(defined('CURL_HTTP_VERSION_2_0')){$device_token = '...';$pem_file = 'pem 文件的路径';$pem_secret = '你的 pem 秘密';$apns_topic = '你的 apns 主题.可以是您的应用程序包 ID';$sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';$url = "https://api.development.push.apple.com/3/device/$device_token";$ch = curl_init($url);curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);$response = curl_exec($ch);$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//在成功响应时,您应该在响应中获得 true 并且状态代码为 200//响应和状态代码列表可在//https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1var_dump($response);var_dump($httpcode);}

I started creating some code based upon this for sending push notifications from PHP.

However now that I have understood there is a new API which utilizes HTTP/2 and provides feedback in the response, I am trying to figure out what I need to do to get that feedback.

I haven't been able to find any tutorials or sample code to give me direction (I guess because it is so new).

Is it possible to use the stream_socket_client() method of connecting to APNS with the new provider API? How do I get the feedback? All I get back from fwrite($fp, $msg, strlen($msg)) right now is a number. For all intents and purposes, you can consider my code the same as the code from the SO question I based my code on

Thanks!

解决方案

With the new HTTP/2 APNS provider API, you can use curl to send push notifications.

EDIT

Before proceeding (as noted by @Madox), openssl >= 1.0.2e of should be installed (from package preferably). Verify with the command

openssl version

a) Your version of PHP should be >= 5.5.24 so that the constant CURL_HTTP_VERSION_2_0 is defined.

b) Make sure that you have curl version 7.46+ installed in your system with

curl --version

c) Curl should have http/2 support enabled. In the output when typing the previous command, you should see a line like this one:

Features: IDN IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets 

if HTTP2 doesn't show up, you can follow this excellent tutorial to install http/2 for curl https://serversforhackers.com/video/curl-with-http2-support

Verify that curl detected openssl >= 1.0.2e, doing curl --version should output something like this:

curl 7.47.1 (x86_64-pc-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.2f zlib/1.2.8 libidn/1.28 nghttp2/1.8.0-DEV librtmp/2.3

e) Once you everything installed, you can test it in the command line:

curl -d '{"aps":{"alert":"hi","sound":"default"}}' \ 
--cert <your-certificate.pem>:<certificate-password> \ 
-H "apns-topic: <your-app-bundle-id>" \ 
--http2  \ 
https://api.development.push.apple.com/3/device/<device-token>

f) Here is a sample code in PHP that I have successfully tried:

if(defined('CURL_HTTP_VERSION_2_0')){

    $device_token   = '...';
    $pem_file       = 'path to your pem file';
    $pem_secret     = 'your pem secret';
    $apns_topic     = 'your apns topic. Can be your app bundle ID';


    $sample_alert = '{"aps":{"alert":"hi","sound":"default"}}';
    $url = "https://api.development.push.apple.com/3/device/$device_token";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $sample_alert);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
    curl_setopt($ch, CURLOPT_SSLCERT, $pem_file);
    curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pem_secret);
    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    //On successful response you should get true in the response and a status code of 200
    //A list of responses and status codes is available at 
    //https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

    var_dump($response);
    var_dump($httpcode);

}

这篇关于新的 APNS 提供程序 API 和 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆