简单的格式APN [英] simple format apn

查看:170
本文介绍了简单的格式APN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在苹果推送通知服务指南阅读:

I read in apple push notification service guidelines that:

通过简单的格式,如果你发送通知信息包是
  畸形以某种方式,例如,有效负载超过规定
  涨停的APN通过切断连接响应。

With the simple format, if you send a notification packet that is malformed in some way—for example, the payload exceeds the stipulated limit—APNs responds by severing the connection.

但是,如果消息(简单的格式)是正确的?将十个分量的APN的连接,如果我发送keep alive包?我不希望建立一个非常大的数量的连接,因为这可能会被视为DOS

But what if the message (simple format) is correct? Will APNs mantain the connection if I send keep alive packets? I don't want to establish a very large number of connections because this may be seen as DOS.

推荐答案

我的意见散列后续版本,更多的细节:

Hashed up version of my comments, with more detail:


  • 打开到APNS服务器的单个连接,连接到您的SQL(或类似)的设备ID的数据库(正式名称:设备道理,不UDID),并遍历这些 - 建立有效载荷,然后发送该给APNS服务器。

  • 您也可以考虑检查你有多远沿着你的设备列表,这样,如果连接你和APNS服务器(或别的东西时)之间切断,那么你可以尝试一次。

  • 如果确实发生了错误,切断连接之前APNS服务器会返回一个错误响应报文。更多相关信息可以在表5-1中找到的文档中,<一个href=\"http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html\"相对=nofollow>链接这里。

  • Open a single connection to the APNS server, connect to your SQL (or similar) database of device ID's (officially: device token, not UDID) and iterate through these - creating the payload and then sending this to the APNS server.
  • You may also consider checking how far along your device list you are, so that if the connection is severed between you and the APNS server (or something else occurs) then you can try again.
  • If an error does occur, before severing the connection the APNS server will return an error-response packet. More information on this can be found in Table 5-1 of the docs, link here.

下面的细节在我的第一点的例子:

Here's a sample of the details in my first point:

// connect to your MySQL database
$con = mysql_connect("localhost", "username", "password");

// select a database
mysql_select_db("my_database", $con);

// run a query to grab your device tokens
$result = mysql_query("SELECT device_tokens FROM some_table");

// set your message
$msg = 'important update';

// create the payload
$body['aps'] = array('alert' => array('body' => $msg, 'action-loc-key' => 'Read'));

// convert to JSON
$payload = json_encode($body);

// setup APNS connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');

// open a connection to the APNS server
$apns = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

while ($row = mysql_fetch_array($result))
{
    $deviceToken = $row['device_tokens'];
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
    fwrite($apns, $apnsMessage, strlen($apnsMessage));
}

// close APNS connection
fclose($apns);

// close database connection
mysql_close($con);

...并记得在URL中的code以上的沙箱或实时推送服务器之间进行切换。

... and remember to switch between sandbox or live push servers in the URL in the code above.

这篇关于简单的格式APN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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