Detailed APNS apple message push notification

Source: Internet
Author: User
Tags htons

What does the Apple message tell you?

As shown


It is the service technology of the app that notifies the user of the app when the user does not open the app process.

Related concepts

Provider: A third-party service system that provides push

Device: Apple devices, such as iphone and ipad

APNS: Apple push messaging service, belonging to Apple

Apps: Apps installed on an Apple device

Devicetoken: Identification of the device used to determine which device and app to receive notifications

Payload: The transmission form of push messages

Overall

From can be seen between provider and APNs is to establish a connection between the APNs and device is to establish a connection between the two connections are encrypted, the use of TLS, the encryption certificate is on the Apple official website to purchase the generated, this certificate in two kinds, one is "developer certificate" , the app that uses the developer certificate can be published on AppStore, the usage fee of developer certificate is $99 per year, the other is "Enterprise Certificate", the app that uses enterprise certificate can only be sent in the enterprise internal market, cannot be sent on AppStore, the annual fee of Enterprise certificate is $299, The Enterprise certificate has no limit on the number of installs, but the developer certificate has a limit on the number of apps installed, but there are no restrictions on installing from AppStore.

How secure connections are built


What if I get the devicetoken of the recipient?

How does provider get Devicetoken? Specific:



Get Devicetoken by App by registering the service and then pass it to provider. Since this devicetoken will change, it is best to register the app every time you start to get the latest Devicetoken

The official code for getting Devicetoken under iOS registration is given below:

-(void) applicationdidfinishlaunching: (UIApplication *) app {//other setup tasks here .... [[UIApplication sharedapplication]registerforremotenotificationtypes: (Uiremotenotificationtypebadge | Uiremotenotificationtypesound)];} Delegation methods-(void) Application: (UIApplication *) Appdidregisterforremotenotificationswithdevicetoken: ( NSData *) Devtoken {const void *devtokenbytes = [Devtoken bytes];self.registered = yes;[ Self sendproviderdevicetoken:devtokenbytes]; Custom method}-(void) Application: (UIApplication *) Appdidfailtoregisterforremotenotificationswitherror: (NSError *) Err {NSLog (@ "Error in registration. Error:%@ ", err);}

How do I send a notification message? Between provider and APNs is a secure socket connection that transmits the binary data in the specified format, as in the following form


Of course, this is the TCP message, but there are several kinds of specific messages, there is the transmission of ordinary messages, there is the transmission form of error response, as well as the form of the message to obtain feedback, for the normal message format as shown:


The first 1 bytes are the command type, 2-3 bytes is the length of the Devicetoken, followed by the concrete value of Devicetoken, followed by the length of the message and the specific contents of the message. After sending the message, you can also view the wrong response before closing the socket, in order to confirm that the message was successfully sent to APNs, and that the error response message is as follows:


Command 8 is the error response of the message, status is the error code, identifier is used to locate the specific push message, this is an enhanced notification of the incoming value, first look at the Apple has a return error code:


Then look at the enhanced notification message format for identifier incoming use:



The official C + + code for sending ordinary messages is as follows:

static bool Sendpayload (SSL *sslptr, Char *devicetokenbinary, Char *payloadbuff, size_t payloadlength) {bool RTN = Fals    E if (sslptr && devicetokenbinary && payloadbuff && payloadlength) {uint8_t command = 0;/* Command n Umber */char binarymessagebuff[sizeof (uint8_t) + sizeof (uint16_t) +device_binary_size + sizeof (uint16_t) + MAXPAYLOAD_ size];/* message format is, | command| tokenlen| token| payloadlen| payload| */char *binarymessagept = binarymessagebuff;uint16_t networkordertokenlength = htons (device_binary_size); uint16_t Networkorderpayloadlength = htons (payloadlength);/* Command */*binarymessagept++ = command;/* token length network order */memcpy (binarymessagept, &networkordertokenlength, sizeof (uint16_t)), binarymessagept + = sizeof (uint16_t);/* Device token */memcpy (binarymessagept, Devicetokenbinary, device_binary_size); Binarymessagept + = Device_binary_size ;/* Payload Length Network order */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 } return RTN;}

Code to send an enhanced notification message:

static bool Sendpayload (SSL *sslptr, Char *devicetokenbinary, Char *payloadbuff, size_t payloadlength) {bool RTN = Fals    E if (sslptr && devicetokenbinary && payloadbuff && payloadlength) {uint8_t command = 1;/* Command n Umber */char binarymessagebuff[sizeof (uint8_t) + sizeof (uint32_t) + sizeof (uint32_t) + sizeof (uint16_t) + Device_binary_ SIZE + sizeof (uint16_t) + maxpayload_size];/* message format is, | command|id| expiry| tokenlen| token| payloadlen| Payload|*/char *binarymessagept = binarymessagebuff;uint32_t whicheverorderiwanttogetbackinaerrorresponse_id = 1234; uint32_t NETWORKORDEREXPIRYEPOCHUTC = htonl (Time (NULL) +86400); Expiremessage if not delivered in 1 dayuint16_t networkordertokenlength = htons (device_binary_size); uint16_t networkOr Derpayloadlength = htons (payloadlength);/* Command */*binarymessagept++ = command;/* provider preference ordered ID */mem CPY (Binarymessagept, &whicheverorderiwanttogetbackinaerrorresponse_id,sizeof (uint32_t)); BinaryMEssagept + = sizeof (uint32_t);/* expiry date Network order */memcpy (Binarymessagept, &NETWORKORDEREXPIRYEPOCHUTC, sizeof (uint32_t)); Binarymessagept + = sizeof (uint32_t);/* Token length network order */memcpy (Binarymessagept & Networkordertokenlength, sizeof (uint16_t)); binarymessagept = sizeof (uint16_t);/* Device Token */memcpy ( Binarymessagept, Devicetokenbinary, device_binary_size); Binarymessagept + = device_binary_size;/* Payload length Network Order */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; } return RTN;}
What is a corner mark (Badge)?

Another important concept is the angular mark, as follows:


This angle is sent in the push message, you can specify, this depends on the specific business how to set, to remind users of the number of unread messages.

At the end of the understanding of the above principles and processes, I believe in what language can be written provider program, I am based on the Java language also simple write a demo, can support the push of multiple applications, and so perfect can put GitHub

Detailed APNS apple message push notification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.