简单格式apn [英] simple format apn

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

问题描述

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

I read in apple push notification service guidelines that:

使用简单的格式,如果你发送一个通知包以某种方式畸形——例如,有效载荷超过规定的限制——APNs 通过切断连接来响应.

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 会保持连接吗?我不想建立大量的连接,因为这可能会被视为 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 服务器的单个连接,连接到设备 ID(正式:设备令牌,而不是 UDID)的 SQL(或类似)数据库并遍历这些 - 创建有效负载,然后将其发送到 APNS 服务器.
  • 您还可以考虑检查您在设备列表中的位置,以便如果您与 APNS 服务器之间的连接中断(或发生其他情况),您可以重试.
  • 如果确实发生错误,则在断开连接之前,APNS 服务器将返回错误响应数据包.有关更多信息,请参见文档的表 5-1,此处链接.

这是我第一点中的细节示例:

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 中在沙盒或实时推送服务器之间切换.

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

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

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