Ios:apns Push the main code

Source: Internet
Author: User

First, in the APPDELEGATE.M:

1, registration notice
//[OBJC] View plaincopyprint? To see code slices on codes to derive from my code slices-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after application launch. Viewcontroller *mainctrl=[[Viewcontroller alloc] init]; Self.window.rootViewController=Mainctrl; //Registration Notice    if([Uidevice currentdevice].systemversion.doublevalue<8.0{[[UIApplication sharedapplication] registerforremotenotificationtypes: (Uiremotenotificationtypealert | Uiremotenotificationtypesound |Uiremotenotificationtypebadge)]; }      Else{[[uiapplication sharedapplication] registerforremotenotifications]; [[UIApplication sharedapplication] registerusernotificationsettings:[uiusernotificationsettings settingsForTypes: Uiusernotificationtypebadge| uiusernotificationtypesound|Uiusernotificationtypealert Categories:nil]]; }            //determines whether the application starts by a remote message notification trigger    if(launchoptions) {//gets the number of application message notification tokens (that is, the number in the small red circle)Nsinteger badge =[UIApplication Sharedapplication].applicationiconbadgenumber; if(badge>0) {              //if the number of application message notification tokens (that is, the number in the small red circle) is greater than 0, clear the marker. badge--; //clears the tag.  Clear the small red circle in the number, the small red circle in the number of 0, the small red circle will be eliminated. [UIApplication Sharedapplication].applicationiconbadgenumber =badge; Nsdictionary*pushinfo = [Launchoptions objectforkey:@"Uiapplicationlaunchoptionsremotenotificationkey"]; //Get push DetailsNSString *pushstring = [NSString stringWithFormat:@"%@", [Pushinfo Objectforkey:@"APS"]]; Uialertview*alert=[[uialertview Alloc] Initwithtitle:@"Finish Loaunch"Message:pushstringDelegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil, nil Nil];          [Alert show]; }      }            returnYES;
2, after registration notice, get device token
- (void) Application: (UIApplication *) app Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken {nsstring*token = [NSString stringWithFormat:@"%@", Devicetoken]; NSLog (@"My Token is:%@", token); //device token should be sent to the server side}    - (void) Application: (UIApplication *) app Didfailtoregisterforremotenotificationswitherror: (Nserror *) Error {NSString*ERROR_STR = [NSString stringWithFormat:@"%@", error]; NSLog (@"Failed to get token, error:%@", ERROR_STR); }  
3. Receive push Notifications
- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo {[UIApplication sharedapplication].applicationiconbadgenumber=0;  for(IDKeyinchuserInfo) {NSLog (@"key:%@, Value:%@", Key, [UserInfo Objectforkey:key]); }      /*eg.         Key:aps, value: {alert = "\u8fd9\u662f\u4e00\u6761\u6d4b\u8bd5\u4fe1\u606f";         badge = 1;     Sound = default; }      */Uialertview*alert=[[uialertview Alloc] Initwithtitle:@"Remote Notification"message:userinfo[@"APS"][@"Alert"]Delegate: Nil Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil, nil Nil];  [Alert show]; }  

Note: When the app foreground runs, it calls the remote Notification;app background runtime, clicks the Reminder box, calls the remote notification, clicks the app icon, does not call the remote notification, does not respond; No runtime, click on the Reminder box, finishlaunching, Launchoptions, click the app icon, launchoptions do not pass the parameter, do not call remote notification.

Ios:apns Push the main code

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.