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