The APNs push handler function in IOS is explained in detail

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. The information that the third-party app will push to the user is pushed to Apple server. Apple server then pushes this information to the user's phone via a unified system interface. Let's say that a friend who doesn't know about it can see this article: Step-by-step teaching you to do iOS push

This article focuses on how to handle push messages on the app side.

The main concerns are some of the more important functions, which are in the Appdelegate class:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions
anyone who has done iOS development will be very familiar with this function, which is started at the end of the program. And is about to be executed when it is called, usually some initialization work can be done in this function. The same. The associated initialization of the push also needs to be completed in this section. This part of the work is mainly divided into two parts:
    • type of push:
[[uiapplication sharedapplication] registerforremotenotificationtypes:uiremotenotificationtypebadge | Uiremotenotificationtypesound | Uiremotenotificationalert];
This line of code tells the system that the push message type of the program, which typically contains badge, sounds, and alert notifications。
    • The handler does not have a push message when it starts:
Assuming that the program is executing or that the program is in the background, the task of handling the push message at this time is:

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

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

Finished in. But suppose 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. The following code is a detailed example:

    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 to Apple server, the token is equivalent to the device identification code, each Apple device has a unique token, Apple's server is the token to find the appropriate device, and send 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 function that the program receives a push message during execution, regardless of whether the current program is in the foreground or in the background. According to Apple's official documentation. We recommend that you use

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

-( void " Span style= "Font-family:menlo") Application: (  *) application didreceiveremotenotification: ( Span style= "FONT-FAMILY:MENLO; Color:rgb (112,61,170) ">nsdictionary  *) userInfo actually can receive, do not know what is going on. Hope prawn troubleshoot ).

The other is -(void) application: (uiapplication *) application Didreceiveremotenotification: (nsdictionary *) userInfo Fetchcompletionhandler: (void (^) (uibackgroundfetchresult)) Completionhandler another function. According to the Apple-given document, the system gives 30s of time to process the push message, and then executes the Completionhandler block.

When processing this analogy sends a message (that is, a push message is received when the program is started). Generally encounter this problem, that is, the current push message is the current program is in the foreground of the execution of the received or the program is executed in the background, the user clicked the System message notification bar corresponding entry procedures received? This fact is very easy and can be solved with the following code:

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            }}


about the structure of userinfo, the official structure of Apple:

{
    "APS": {
        "Alert": "You got your emails.",
        "Badge": 9,
        "Sound": "Bingbong.aiff"
    },
    "Acme1": "Bar",
    "Acme2": 42
}
that is, key APS has a dictionary corresponding to it. Inside is the details of the push message. Details are related to the push type of our brochure.

The rest of the key is the user's own definition.



The APNs push handler function in IOS is explained in detail

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.