您的位置:首页 > 其它

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,asdescribedinThe
BinaryInterfaceandNotificationFormat.

Theseformatsdonotincludeapriority;apriorityof
10
isassumed.


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;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: