An explanation of the APNs push processing function under IOS

Source: Internet
Author: User

There is no doubt that the practice is better than android,ios in pushing. APNS (Apple push Notification service) is a messaging push from Apple. The principle is that third-party applications will be pushed to the user's information pushed to the Apple server, the Apple server through a unified system interface to push this information to the user's mobile phone. If you do not give up understanding of the friends can see this article: Step by step to teach you to do iOS push

This article focuses on how to handle push messages on the app side. The main concerns are several more important functions, all of which are in the Appdelegate class:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions
iOS developers are familiar with this function, which is called when the program ends and is about to run, and usually some initialization work can be done in this function. Similarly, the associated initialization of the push needs to be done in this section. This part of the work is mainly divided into two parts:
    • Registration of push types:
[[uiapplication sharedapplication] registerforremotenotificationtypes:uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationalert];
This line of code tells the system that the type of push message registered by the program, usually including badge, sounds, and alert notifications。
    • The handler does not have a push message when it starts:
if the program is running or the program is in the background, then this time the work of handling the push message is:

-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo or:

-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler

completed in. However, if the user clicks the push notification when the program has not been started, this time the above two functions are not receiving the user's push notification, this time need to application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary*) launchoptions function inside the processing. The information about the push message is stored in the Launchoptions dictionary. Refer to the following code:

    nsdictionary* pushinfo = [launchoptions objectforkey:@ "Uiapplicationlaunchoptionsremotenotificationkey"];    if (pushinfo)    {        nsdictionary *apsinfo = [Pushinfo objectforkey:@ "APS"];        if (apsinfo)        {            //your code here        }            }


-(void) Application: (UIApplication *) Applicationdidregisterforremotenotificationswithdevicetoken: (NSData *) PToken & -(void) Application: (UIApplication *) applicationdidfailtoregisterforremotenotificationswitherror :(Nserror *) Error in order for the device side to receive the push message, it is necessary to transfer the token of the equipment to Apple's server, the token is equivalent to the device's identification code, each Apple device has a unique token, Apple's server is through this token to find the corresponding device, and transmits the corresponding message. These two functions are called after the token succeeds or fails, and the user does something corresponding to the corresponding function.
- (void) Application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfo

-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo and

-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler

is a handler for a push message that is received by the program during the run, regardless of whether the current program is in the foreground or in the background. According to Apple's official documentation, it is recommended that you use

-(void) Application: (uiapplication *) application didreceiveremotenotification: (nsdictionary *) UserInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler

Because the former can not receive the push message when the program is in the background (measured-(void) Application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfoactually can receive, do not know what is going on, hope prawn troubleshoot)。 Another is-(void) Application: (uiapplication*) Application didreceiveremotenotification: (nsdictionary*) UserInfo Fetchcompletionhandler: (void (^)(Uibackgroundfetchresult)) CompletionhandlerThere is also a role. According to the Apple-given document, the system gives 30s of time to process the push message and then runs the Completionhandler block.

When processing this send message (that is, the program is launched after receiving a push message), usually encounter such a problem, that is, the current push message is the current program is running in the foreground or the program is running in the background, the user clicked the System message notification bar corresponding entry procedures received? This is actually very simple, with the following code can be resolved:

void application: (uiapplication*) application didreceiveremotenotification:nsdictionary) UserInfo Fetchcompletionhandler: ((^) uibackgroundfetchresult) completionhandler{if (application.applicationstate = = uiapplicationstateactive) {        NSLog (@ "active");        The program is currently in    the foreground}    else if (application.applicationstate = = uiapplicationstateinactive)    {        NSLog (@ " Inactive ");        The program is in the background            }}


For the structure of UserInfo, refer to Apple's official structure:

{
    "APS": {
        "Alert": "You got your emails.",
        "Badge": 9,
        "Sound": "Bingbong.aiff"
    },
    "Acme1": "Bar",
    "Acme2": 42
}
that is, the key APS corresponds to a dictionary, which is the specific information of the push message. Specific to our registered push type. Some of the remaining keys are user-definable.



An explanation of the APNs push processing function under IOS

Related Article

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.