apns 官方文档
2015-09-07 16:59
183 查看
原文地址:https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/LegacyFormat.html#//apple_ref/doc/uid/TP40008194-CH105-SW1
LegacyInformation
NewdevelopmentshouldusethemodernformattoconnecttoAPNs,asdescribedinTheBinaryInterfaceandNotificationFormat.
Theseformatsdonotincludeapriority;apriorityof
10isassumed.
SimpleNotificationFormat
FigureA-1showsthisformat.FigureA-1Simplenotificationformat
Thefirstbyteinthelegacyformatisacommandvalueof0(zero).Theotherfieldsarethesameastheenhancedformat.Listing
A-1givesanexampleofafunctionthatsendsaremotenotificationtoAPNsoverthebinaryinterfaceusingthesimplenotificationformat.TheexampleassumespriorSSLconnectionto
gateway.push.apple.com(or
gateway.sandbox.push.apple.com)
andpeer-exchangeauthentication.
ListingA-1Sendinganotificationinthesimpleformatviathebinaryinterface
staticboolsendPayload(SSL*sslPtr,char*deviceTokenBinary,char*payloadBuff,size_tpayloadLength) |
{ |
boolrtn=false; |
if(sslPtr&&deviceTokenBinary&&payloadBuff&&payloadLength) |
{ |
uint8_tcommand=0;/*commandnumber*/ |
charbinaryMessageBuff[sizeof(uint8_t)+sizeof(uint16_t)+ |
DEVICE_BINARY_SIZE+sizeof(uint16_t)+MAXPAYLOAD_SIZE]; |
/*messageformatis,|COMMAND|TOKENLEN|TOKEN|PAYLOADLEN|PAYLOAD|*/ |
char*binaryMessagePt=binaryMessageBuff; |
uint16_tnetworkOrderTokenLength=htons(DEVICE_BINARY_SIZE); |
uint16_tnetworkOrderPayloadLength=htons(payloadLength); |
/*command*/ |
*binaryMessagePt++=command; |
/*tokenlengthnetworkorder*/ |
memcpy(binaryMessagePt,&networkOrderTokenLength,sizeof(uint16_t)); |
binaryMessagePt+=sizeof(uint16_t); |
/*devicetoken*/ |
memcpy(binaryMessagePt,deviceTokenBinary,DEVICE_BINARY_SIZE); |
binaryMessagePt+=DEVICE_BINARY_SIZE; |
/*payloadlengthnetworkorder*/ |
memcpy(binaryMessagePt,&networkOrderPayloadLength,sizeof(uint16_t)); |
binaryMessagePt+=sizeof(uint16_t); |
/*payload*/ |
memcpy(binaryMessagePt,payloadBuff,payloadLength); |
binaryMessagePt+=payloadLength; |
if(SSL_write(sslPtr,binaryMessageBuff,(binaryMessagePt-binaryMessageBuff))>0) |
rtn=true; |
} |
returnrtn; |
} |
EnhancedNotificationFormat
Theenhancedformathasseveralimprovementsoverthesimpleformat:Errorresponse.Withthesimpleformat,ifyousendanotificationpacketthatismalformedinsomeway—forexample,thepayloadexceedsthestipulated
limit—APNsrespondsbyseveringtheconnection.Itgivesnoindicationwhyitrejectedthenotification.Theenhancedformatletsaprovidertaganotificationwithanarbitraryidentifier.Ifthereisanerror,APNsreturnsapacketthatassociatesanerror
codewiththeidentifier.Thisresponseenablestheprovidertolocateandcorrectthemalformednotification.
Notificationexpiration.APNshasastore-and-forwardfeaturethatkeepsthemostrecentnotificationsenttoanapponadevice.Ifthedeviceisoffline
attimeofdelivery,APNsdeliversthenotificationwhenthedevicenextcomesonline.Withthesimpleformat,thenotificationisdeliveredregardlessofthepertinenceofthenotification.Inotherwords,thenotificationcanbecome“stale”overtime.The
enhancedformatincludesanexpiryvaluethatindicatestheperiodofvalidityforanotification.APNsdiscardsanotificationinstore-and-forwardwhenthisperiodexpires.
FigureA-2depictstheformatfornotificationpackets.
FigureA-2Enhancednotificationformat
Thefirstbyteinthenotificationformatisacommandvalueof1.Theremainingfieldsareasfollows:
Identifier—Anarbitraryvaluethatidentifiesthisnotification.Thissameidentifierisreturnedinaerror-responsepacketifAPNscannotinterpreta
notification.
Expiry—AfixedUNIXepochdateexpressedinseconds(UTC)thatidentifieswhenthenotificationisnolongervalidandcanbediscarded.Theexpiryvalue
usesnetworkbyteorder(bigendian).Iftheexpiryvalueisnon-zero,APNstriestodeliverthenotificationatleastonce.SpecifyzerotorequestthatAPNsnotstorethenotificationatall.
Tokenlength—Thelengthofthedevicetokeninnetworkorder(thatis,bigendian)
Devicetoken—Thedevicetokeninbinaryform.
Payloadlength—Thelengthofthepayloadinnetworkorder(thatis,bigendian).Thepayloadmustnotexceed256bytesandmustnotbenull-terminated.
Payload—Thenotificationpayload.
ListingA-2composesaremotenotificationintheenhancedformatbeforesendingittoAPNs.Itassumes
priorSSLconnectionto
gateway.push.apple.com(or
gateway.sandbox.push.apple.com)andpeer-exchange
authentication.
ListingA-2Sendinganotificationintheenhancedformatviathebinaryinterface
staticboolsendPayload(SSL*sslPtr,char*deviceTokenBinary,char*payloadBuff,size_tpayloadLength) |
{ |
boolrtn=false; |
if(sslPtr&&deviceTokenBinary&&payloadBuff&&payloadLength) |
{ |
uint8_tcommand=1;/*commandnumber*/ |
charbinaryMessageBuff[sizeof(uint8_t)+sizeof(uint32_t)+sizeof(uint32_t)+sizeof(uint16_t)+ |
DEVICE_BINARY_SIZE+sizeof(uint16_t)+MAXPAYLOAD_SIZE]; |
/*messageformatis,|COMMAND|ID|EXPIRY|TOKENLEN|TOKEN|PAYLOADLEN|PAYLOAD|*/ |
char*binaryMessagePt=binaryMessageBuff; |
uint32_twhicheverOrderIWantToGetBackInAErrorResponse_ID=1234; |
uint32_tnetworkOrderExpiryEpochUTC=htonl(time(NULL)+86400);//expiremessageifnotdeliveredin1day |
uint16_tnetworkOrderTokenLength=htons(DEVICE_BINARY_SIZE); |
uint16_tnetworkOrderPayloadLength=htons(payloadLength); |
/*command*/ |
*binaryMessagePt++=command; |
/*providerpreferenceorderedID*/ |
memcpy(binaryMessagePt,&whicheverOrderIWantToGetBackInAErrorResponse_ID,sizeof(uint32_t)); |
binaryMessagePt+=sizeof(uint32_t); |
/*expirydatenetworkorder*/ |
memcpy(binaryMessagePt,&networkOrderExpiryEpochUTC,sizeof(uint32_t)); |
binaryMessagePt+=sizeof(uint32_t); |
/*tokenlengthnetworkorder*/ |
memcpy(binaryMessagePt,&networkOrderTokenLength,sizeof(uint16_t)); |
binaryMessagePt+=sizeof(uint16_t); |
/*devicetoken*/ |
memcpy(binaryMessagePt,deviceTokenBinary,DEVICE_BINARY_SIZE); |
binaryMessagePt+=DEVICE_BINARY_SIZE; |
/*payloadlengthnetworkorder*/ |
memcpy(binaryMessagePt,&networkOrderPayloadLength,sizeof(uint16_t)); |
binaryMessagePt+=sizeof(uint16_t); |
/*payload*/ |
memcpy(binaryMessagePt,payloadBuff,payloadLength); |
binaryMessagePt+=payloadLength; |
if(SSL_write(sslPtr,binaryMessageBuff,(binaryMessagePt-binaryMessageBuff))>0) |
rtn=true; |
} |
returnrtn; |
} |
相关文章推荐
- Java中枚举的7种用法
- 《IOS_OC》继承、多态、初始化方法、便利构造器
- IOS_UI_UITableView 应用
- 蓝桥杯:翻硬币
- python中的高阶函数
- Sql数据库批量清理日志
- mysql中insert和replace的区别
- 第一周 项目1 C++语言中函数参数传递的三种方式
- 第二周项目0--宣告“主权”
- C++容易被忽略的法则
- S3C44b0x通用延时函数,延时time个100us函数理解
- 1230 -- 素数环问题
- hdu4508
- 合并两个排序的链表(剑指offer)
- 20150907数据类型与数据转换
- CAPI3 HTTP文件服务器搭建(共享目录版)
- Redis学习笔记(六)--消息通知
- LSBInitScripts
- 如何安装Parallels Tools
- 黑马程序员---JAVA反射机制 二