Apple native push

Source: Internet
Author: User

A long time ago to write the demo, now remember the next ~ ~ ~ may be a mistake

Github:https://github.com/bigshow1949/yfpushtest

Steps:

1) Add App ID.

Apple Developer Center: Identifiers--->app IDs to add an ID, such as my name:yfpushtest, where the ID must be the project bundle Identifier, such as: com. YFPushTest.BigShow.YFPushTest;

2) Generate Apple Push certificate (production & test)

Named for example: COM. YFPushTest.BigShow.YFPushTest, choose the app ID, to choose the one we just created, do not choose the other (many AppID, easy to forget)

3) Generate a description file

Name: Yfpushtestprofile, again, AppID don't choose the wrong

4) on the code:

1 #import "AppDelegate.h"2 3 @interfaceappdelegate ()4 5 @end6 7 @implementationappdelegate8 9 Ten-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions One { A     //application.applicationiconbadgenumber = 0; -      -     if([Uidevice Currentdevice].systemversion.doublevalue >=8.0) the     { -         //1. Request authorization to send notifications to users -Uiusernotificationsettings *settings = [Uiusernotificationsettings settingsfortypes:uiusernotificationtypesound | Uiusernotificationtypebadge |Uiusernotificationtypealert Categories:nil]; -          + [Application registerusernotificationsettings:settings]; -     } +     Else A     { at[Application Registerforremotenotificationtypes:uiremotenotificationtypealert | Uiremotenotificationtypebadge |Uiremotenotificationtypesound]; -     } -      -     if(Launchoptions[uiapplicationlaunchoptionsremotenotificationkey]) -     { -         //Jump in     } -      to     returnYES; + } -  the  * /** $ * Remote Push registration succeededPanax Notoginseng  * - * @param devicetoken devicetoken the  */ +- (void) Application: (UIApplication *) application Didregisterforremotenotificationswithdevicetoken: (NSData *) Devicetoken A { the     //25bb75ac 3ffcebd7 90d9f517 1ebca904 154a367a 87781e5d b9ea288e 37fdf487 +NSLog (@"-----devicetoken--------%@ devdesc---%@", devicetoken,devicetoken.description); -      $     //TOKENSTR get the tokens available.  $NSString *tokenstr = [NSString stringWithFormat:@"%@", Devicetoken]; -TOKENSTR = [Tokenstr stringbytrimmingcharactersinset:[nscharacterset charactersetwithcharactersinstring:@"<>"]];//Remove the <> -TOKENSTR = [Tokenstr stringbyreplacingoccurrencesofstring:@" "Withstring:@""];//Remove the blanks the      -NSLog (@"Token---%@", tokenstr);Wuyi //    //registration Successful, return token the //Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "APNs return token:" Message:tokenstr delegate:self Cancelbutt ontitle:@ "Cancel" otherbuttontitles:@ "OK", nil]; - //     Wu //[alert show]; -      About     //Applicationiconbadgenumber $ } -  -  - /** A * Registration Failed +  * the  */ -- (void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (NSError *) Error $ { the      theNSLog (@"registration failed----%@", error); theNSString *tokenstr = [NSString stringWithFormat:@"%@", error]; theUialertview *alert = [[Uialertview alloc] Initwithtitle:@"Registration failed!"Message:tokenstrDelegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; - [alert show]; inNSLog (@"Registration failed%@", error); the      the      About}//Ns_available_ios (3_0); the  the  the  +- (void) Application: (UIApplication *) application didreceiveremotenotification: (Nsdictionary *) UserInfo - { theNSLog (@"receive push notifications----%@", userInfo);Bayi      the     /* the       - "name": "Yfpushtest", - " Action_type": "1", the " message": "Your vehicle jing kkkkkk in 2016-4-22 into Handan", the "APS": { the "alert": "This is some fancy message.", the "badge": 1, - "Sound ": "Your vehicle jing kkkkkk in 2016-4-22 into Handan" the      }; the       the      */94      the     //Application.applicationiconbadgenumber-=1; the     //renders a push message as alert theNSString *message = [[UserInfo objectforkey:@"APS"]objectforkey:@"Alert"];98      AboutUialertview *alert = [[Uialertview alloc] Initwithtitle:@"Tips"Message:messageDelegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:@"Determine", nil]; -     101 [alert show];102 }103 104  the //This callback would be made upon calling-[uiapplication Registerusernotificationsettings:]. The settings the user has granted to the application would be a passed in as the second argument.106- (void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings *) Notificationsettings107 {108     109NSLog (@"ls----%@", notificationsettings); the     111     //2. Registering a remote notification the [Application registerforremotenotifications];113      the}//Ns_available_ios (8_0) __tvos_prohibited; the  the 117 //called when your app had been activated by the user selecting an action from a remote notification.118 //A Nil Action identifier indicates the default action.119 //You should call the completion handler as soon as you ' ve finished handling the action. -- (void) Application: (UIApplication *) application Handleactionwithidentifier: (Nullable NSString *) identifier Forremotenotification: (nsdictionary *) userInfo withresponseinfo: (nsdictionary *) Responseinfo Completionhandler: (void(^) ()) Completionhandler121 {122     123NSLog (@"iOS9 Added---%@--%@---%@", identifier,userinfo,responseinfo);124      the     126}//Ns_available_ios (9_0) __tvos_prohibited;127  - 129 //called when your app had been activated by the user selecting an action from a remote notification. the //A Nil Action identifier indicates the default action.131 //You should call the completion handler as soon as you ' ve finished handling the action. the- (void) Application: (UIApplication *) application Handleactionwithidentifier: (Nullable NSString *) identifier Forremotenotification: (nsdictionary *) UserInfo Completionhandler: (void(^) ()) Completionhandler133 {134     135NSLog (@"iOS8 Added---%@--%@---%@", identifier,userinfo,application);136     137     138}//Ns_available_ios (8_0) __tvos_prohibited;139  $ 141 142 143 @end

5) All done well, we have to simulate sending push. Here we use this test program: Https://github.com/shaojiankui/SmartPush Download the push certificate We just generated, and then pull it over:

Then connect to the server, then click Push to Ok~~~~~~

Problems that may occur:

1) Registration failed error Domain=nscocoaerrordomain code=3000 "The authorization string for" Aps-environment "of the application not found"

This is because the push certificate setting is wrong. And will invoke remote push registration failed This method (-(void) Application: (UIApplication *) application Didfailtoregisterforremotenotificationswitherror: (Nserror *) error); First check whether the AppID of the three certificates is the right one, and whether the three certificates are double-clicked; AppID ID is the bundle Identifier of the project; Basically also in these cases, there are other conditions can be commented to add.

Apple native push

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.