Local and Push Notification Programming Guide

这章描述了接口,这些接口是开发者用于和APNs服务器通信的,讨论了一些功能,这些功能是开发者期望能够达到的。

一般的开发者的要求

##作为一个开发者你和APNs服务器的通信是通过一个二进制接口。这个接口对于开发者是一个高速度、高能量的,它设计与二进制内容使用TCP连接流。二进制接口是异步的。

##产品环境(即产品发布)的二进制接口通过gateway.push.apple.com,端口号为2195是有效的;开发环境(产品测试)的二进制接口通过gateway.sandbox.push.apple.com,端口号为2195是有效的。

##对于每一个接口,使用TLS(或者SSL)来创建一个安全的通信渠道。这些连接所需的SSL证书是通过IOS Provisioning Portal配置的。为了创建一个信任的开发者标识,在连接时现在这个证书到APNs使用对等认证。

##注意:为了创建一个和APNs的TLS会话,Entrust Secure CA呼吁开发者服务器一定要安装证书。如果服务器运行在OS X,这个根证书已经在钥匙链。在其他系统中,证书可能是无效的。你可以从Entrust SSL Certificates的网站上下载证书。

##作为一个开发者,你负责一下推送通知方面:

##1.你必须通知有效负荷。

##2.你负责向应用程序图标的展示提供标记号码。

##3.定期的和反馈服务连接,获得当前的已经重复报告发送失败的设备列表。然后停止和应用关联的设备发送通知。

##如果你打算在多种语言中支持通知消息,但是没有使用aps的有效负荷的loc-key和loc-args特性,为了客户端读取本地化字符串,你需要在服务器端警报消息文本。为了这个,你需要从客户端应用找到当前语言的优先级。

##连接管理最好的实践

###你可能创建多个连接到同一个入口或者到多个入口的实例。如果你需要发送大量的推送通知,把他们分散到不同的入口。这样比使用一个连接能够提供性能。它让你发送推送通知更快,让APNs发送更快。

###通过多个通知保持你和APNs的连接,不要反复的打开和关闭连接。这样APNs会认为快速的连接和关闭为拒绝服务袭击。你应该剩下一个打开的连接除非你知道它在很长一段时间是空闲的,例如,如果你仅仅一天发送一次通知给你的用户,那么你就每天使用一个新的连接。


##二进制接口和通知格式

###二进制接口因为二进制内容使用TCP连接,本质上是一种流。为了最佳的性能,批处理多个通知在一个单一的传输接口,无论是显式或使用TCP/IP Nagle算法。通知的格式如图所示:

##通知格式的顶层的组成如下图:

Field name

Length

Discussion

Command

1 byte

Populate with the number 2.

Frame length

4 bytes

The size of the frame data.

Frame data

variable length

The frame contains the body, structured as a series of items.

frame data是由下面所示组成:

Field name

Length

Discussion

Item ID

1 byte

The item identifier. For example, the item number of the payload is 2.

Item data length

2 bytes

The size of the item data.

Item data

variable length

The value for the item.

每项和他们的标识符如下:

Item ID

Item Name

Length

Data

1

Device token

32 bytes

The device token in binary form, as was registered by the device.

2

Payload

variable length, less than or equal to 256 bytes

The JSON-formatted payload.

The payload must not be null-terminated.

3

Notification identifier

4 bytes

An arbitrary, opaque value that identifies this notification. This identifier is used for reporting errors to your server.

4

Expiration date

4 bytes

A UNIX epoch date expressed in seconds (UTC) that identifies when the notification is no longer valid and can be discarded.

If this value is non-zero, APNs stores the notification tries to deliver the notification at least once. Specify zero to indicate that the notification expires immediately and that APNs should not store the notification at all.

5

Priority

1 byte

The notification’s priority. Provide one of the following values:

  • 10 The push message is sent immediately.

    The push notification must trigger an alert, sound, or badge on the device. It is an error to use this priority for a push that contains only the content-available key.

  • 5 The push message is sent at a time that conserves power on the device receiving it.

如果你发送的通知是APNs可以接受的,那么什么也不返回。

##如果你发送的通知是畸形的或者模糊不清的,APNs返回一个错误响应包,然后关闭连接。任何通知,你使用同一个连接发送畸形通知后,会被丢弃。

Figure 5-2  Format of error-response packet##错误响应代码包:

Status code

Description

0

No errors encountered

1

Processing error

2

Missing device token

3

Missing topic

4

Missing payload

5

Invalid token size

6

Invalid topic size

7

Invalid payload size

8

Invalid token

10

Shutdown

255

None (unknown)

##状态值为10表明APNs服务关闭连接。在错误响应中的通知标识表明了最后的通知成功的发送了。任何你发送的通知在被丢弃后你一定是愤怒的。当你接收这个状态代码时,停止使用这个连接,然后打开一个新的连接。

##主要在产品环境中的设备标志和开发环境中的设备标识是不一样的值。


##反馈服务

###APNs包括一个反馈服务器为了给你关于发送通知失败的信息。当你发送通知不能被发出时因为在设备上预期的APP不存在,反馈服务把那个设备的标志添加到他的列表中。使用时间戳来验证设备标志没有再定位由于反馈实体已经产生。对于每一个设备没有再定位,停止发送通知。APNs监听器提供经常检查反馈服务,控制向设备上不存在的应用发送推送通知。

##注意:反馈服务为每一个推送主题包含一个分割列表。如果你有多个app,你一定要为每一个app连接到反馈服务,使用对应的证书,为了接受所有的反馈。

##反馈服务有一个二进制接口类似于发送推送通知的接口。你通过feedback.push.apple.com,端口号2196访问产品访问服务,通过feedback.sandbox.push.apple.com端口号2196访问开发反馈服务。就像推送通知的二进制接口,使用TLS(或者SSL)创建一个安全的通信通道。你使用相同的证书连接反馈服务就像你用来发送通知一样。为了创建一个被信任的开发者标志,在连接时使用证书和APNs的对等认证。

##一旦你连接上了,传送马上开始,你不需要发送任何命令到APNs.从反馈服务中读取流直到这没有更多的可读数据。接收数据的格式如下:

Figure 5-3  Binary format of a feedback tupleBinary format of a feedback tuple

Timestamp

A timestamp (as a four-byte time_t value) indicating when APNs determined that the application no longer exists on the device. This value, which is in network order, represents the seconds since 12:00 midnight on January 1, 1970 UTC.

Token length

The length of the device token as a two-byte integer value in network order.

Device token

The device token in binary format.

##当你读取完之后反馈服务会清除。每次你连接到反馈服务,它都返回信息列表仅仅从你最后一次连接时发生失败的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值