//Local notification callback function that is called when the application is in the foreground- (void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification {NSLog (@"noti:%@", notification); //where the real need to deal with interaction here//get the data with the notificationNSString *key =[Notification.userinfo Objectforkey:localpushkey]; NSString*title = [Notification.userinfo objectforkey:@"title"]; NSString*content = [Notification.userinfo objectforkey:@"content"]; NSString*sysversion =[Uidevice currentdevice].systemversion; CGFloat version=[Sysversion Floatvalue]; if(Version >=8.0) {Uialertcontroller*alertcontroller =[Uialertcontroller alertcontrollerwithtitle:title message:content Preferredstyle:uialertcontrollerstylealert] ; Uialertaction*okaction = [Uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault Handler:nil]; [Alertcontroller addaction:okaction]; [[UIApplication Sharedapplication].keywindow.rootviewcontroller Presentviewcontroller:alertcontroller Animated: YES Completion:nil]; } Else{Uialertview*alert =[[Uialertview alloc] Initwithtitle:title message:content Delegate: nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil]; [Alert show]; } //update the number of badges displayedNsinteger badge =[UIApplication Sharedapplication].applicationiconbadgenumber; Badge--; Badge= Badge >=0? Badge:0; [UIApplication Sharedapplication].applicationiconbadgenumber=badge; //You can cancel the push when no more push is required[AppController Cancellocalnotificationwithkey:key];}//set up local notifications+ (void) Registerlocalnotification: (NSNumber *) alerttime key: (NSString *) key alertbody: (NSString *) alertbody title: (NSString *) title content: (NSString *) content{//NSLog (@ "registerlocalnotification:%long%@%@%@%@", [Alerttime IntegerValue], key, Alertbody, title, cont ENT);Uilocalnotification *notification =[[Uilocalnotification alloc] init]; //set the time to trigger notificationsNSDate *firedate =[NSDate datewithtimeintervalsincenow:[alerttime IntegerValue]; //NSLog (@ "firedate=%@", firedate);notification.firedate=firedate; //Time ZoneNotification.timezone =[Nstimezone Defaulttimezone]; //set the recurrence intervalNotification.repeatinterval =Kcfcalendarunitsecond; //Notification Content//notification.alerttitle = @ "Notification";Notification.alertbody =Alertbody; Notification.applicationiconbadgenumber=1; //sounds that are played when notifications are triggeredNotification.soundname =Uilocalnotificationdefaultsoundname; //Notification ParametersNsdictionary *userdict = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:key, title, content, nil] Forkeys:[nsarray Arraywithobjects:localpushkey,@"title",@"content", Nil]];//[Nsdictionary dictionarywithobject:userdictdata Forkey:localpushkey];Notification.userinfo =userdict; //after iOS8, you need to add this registration to be authorized if([[UIApplication sharedapplication] Respondstoselector: @selector (registerusernotificationsettings:)]) {UIUser NotificationType type= Uiusernotificationtypealert | Uiusernotificationtypebadge |Uiusernotificationtypesound; Uiusernotificationsettings*settings =[Uiusernotificationsettings Settingsfortypes:type Categories:nil]; [[UIApplication sharedapplication] registerusernotificationsettings:settings]; //the unit that notifies the repeating prompt, which can be day, week, monthNotification.repeatinterval =Nscalendarunitday; } Else { //the unit that notifies the repeating prompt, which can be day, week, monthNotification.repeatinterval =Nsdaycalendarunit; } //Perform notification registration[[UIApplication sharedapplication] schedulelocalnotification:notification];}//cancel a local push notification+ (void) Cancellocalnotificationwithkey: (NSString *) Key {//get all local notification arraysNsarray *localnotifications =[UIApplication sharedapplication].scheduledlocalnotifications; for(Uilocalnotification *notificationinchlocalnotifications) {Nsdictionary*userinfo =Notification.userinfo; if(userInfo) {//get notification parameters based on the key specified when setting the notification parametersNSString *userkey =[UserInfo Objectforkey:localpushkey]; //If you find a notification that you want to cancel, cancel if(UserKey! = Nil &&[UserKey Isequaltostring:key]) {[[UIApplication sharedapplication] cancellocalnotification:notification]; Break; } } }}
iOS local push