Introduction to push features: what needs to be done on the app side

Source: Internet
Author: User

Introduction to push Features

The process for implementing the push is as follows:

From the app registration push feature to the APNS server to send push messages to the device, there are five steps.

Once the push registration is complete, the application's own server provides the push as provider.

App-side implementations


In terms of code, push registration, monitoring, and processing are all concentrated in the Appdelegate class:
1. (BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
Two main functions are implemented in the method body:
The first is to complete the Push function registration request, that is, when the program starts to eject whether to use the push function;
Second, the implementation of the program start is triggered by the push message window, where the push content can be processed;
1.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions{self.window=[[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];//Override point for customization after application launch.Self.viewcontroller =[[[ Viewcontroller alloc] init] Autorelease];self.window.rootviewcontroller=Self.viewcontroller; [Self.window setbackgroundcolor:[uicolor colorwithpatternimage:[uiimage imagenamed:@"Background.png"]]]; [Self.window makekeyandvisible]; [[UIApplication Sharedapplication] registerforremotenotificationtypes: (Uiremotenotificationtypealert| Uiremotenotificationtypebadge |Uiremotenotificationtypesound)];//The procedure is not done by the push service, that is, the user clicks the push notification to complete theif(launchoptions) {nsdictionary* Pushnotificationkey =[Launchoptions Objectforkey:uiapplicationlaunchoptionsremotenotificationkey];if(pushnotificationkey) {Uialertview*alert =[[Uialertview alloc]initwithtitle:@] Push notification "message:@" This is a program launched through a push window where you can process the push content "Delegate: nilcancelbuttontitle:@ "Know" otherbuttontitles:nil, nil]; [Alert show]; [Alert release];}}returnYES;}

2. Receive the only device token returned from the Apple server, which is the basis on which the push server sends the push message, so it needs to be sent back to the push server to save

-(void) Application: (UIApplication *) app Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {nsstring* token = [NSString stringWithFormat:@ "%@" Generated devtoken:%@ ", token); // send Devicetoken to our push server devicesender* sender = [[[Devicesender Alloc]initwithdelegate:self]autorelease];[ Sender Senddevicetopushserver:token];}

3. Receive the error when registering the push notification function, and do the related processing:

-(void) Application: (UIApplication *) app Didfailtoregisterforremotenotificationswitherror: (Nserror *  error occurred while registering the push feature, error message: \ n%@ ", err);}

4. Received the push message, parsing processing

- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) Userinfo{nslog (@ "\napnsDidreceiveremotenotification,receive data:\n%@ ", userInfo);//set the tag number on icon to 0,Application.applicationiconbadgenumber =0;if([[UserInfo Objectforkey:@"APS"] objectforkey:@ "alert"]!=NULL) {Uialertview* Alert = [[Uialertview alloc] initwithtitle:@ "* * Push message * *"Message:[[userinfo Objectforkey:@"APS"] objectforkey:@ "alert"]Delegate: selfcancelbuttontitle:@ "Off" otherbuttontitles:@ "Handling push content", Nil];alert.tag=Alert_tag_push; [Alert show];}}


Through the above code, the basic push function development has been completed, need to push the server support, please resolve yourself.

Introduction to push features: what needs to be done on the app side

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.