The method written in this paper mainly applies uilocalnotification to reach local push notification bar information
Canceled the Uialertview method that expired in other tutorials
Use uilocalnotification mainly divided into create call cancel three steps
Also note that if the [NSDate Datewithtimeintervalsince1970:alerttime] method is called, the time is not calculated from the January 1, 1970 start of the display, but on January 1, 1970 8 o'clock
See GMT for details
1. Create uilocalnotification write the following code in the Appdelegate and the controller that implements the notification separately note that the key value in the creation method is the mark used for the subsequent cancellation.
appdelegate- (void) Applicationdidbecomeactive: (UIApplication *) Application {//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface. //Cancel Badge[[UIApplication sharedapplication] Setapplicationiconbadgenumber:0];}#pragmaThe Mark local notification callback function is called when the application is in the foreground-(void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification {//update the number of badges displayedNsinteger badge =[UIApplication Sharedapplication].applicationiconbadgenumber; Badge--; Badge= Badge >=0? Badge:0; [UIApplication Sharedapplication].applicationiconbadgenumber=badge;}
Controller#pragmaMark Local notification function + (void) Registerlocalnotification: (Nsinteger) alerttime {//creating a local notification objectUilocalnotification *notification=[[Uilocalnotification alloc]init]; //set the time to trigger notificationsNSDate *firedate=[NSDate Datewithtimeintervalsince1970:alerttime]; NSLog (@"time to trigger notification =%@", firedate); Notification.firedate=firedate; //Setting the time zoneNotification.timezone=[Nstimezone Defaulttimezone]; //set the recurrence intervalNotification.repeatinterval=Kcfcalendarunitday; //Set notification contentnotification.alertbody=@"Good Morning Oh ~ I miss you today"; Notification.applicationiconbadgenumber=1; //sounds that are played when notifications are triggeredNotification.soundname=Uilocalnotificationdefaultsoundname; //Create info information for local notifications for canceling notificationsNsdictionary *info = [Nsdictionary dictionarywithobject:@"name"Forkey:@"Weather"]; Notification.userinfo=info; //iOS8 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 repeat prompts can be days of the weekNotification.repeatinterval =Nscalendarunitday; } Else { //the unit that notifies repeat prompts can be days of the weekNotification.repeatinterval =Nsdaycalendarunit; } //Perform notification registration[[UIApplication sharedapplication] schedulelocalnotification:notification];}#pragmaMark cancels a local 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 *info =Userinfo[key]; //If you find a notification that you want to cancel, cancel if(Info! =Nil) {[[UIApplication sharedapplication] cancellocalnotification:notification]; Break; } } }}
2. Call Uilocalnotification because the code above encapsulates the calling method as a class method directly with the corresponding controller class
#pragma Mark call Local Notification method-(void) localnotification{ // call Local notification method [Mainviewcontroller registerlocalnotification:p]; NSLog (@ " turn on local notifications ");}
3. Canceling the Uilocalnotification cancellation method Similarly, the invocation of a class method cancels the corresponding notification according to the key value in the method defined
-(void) notificationswitch{if(noticeswitch.on==YES) { //calling local notifications[self localnotification]; NSLog (@"turn on local notifications"); } if(noticeswitch.on==NO) {[Mainviewcontroller Cancellocalnotificationwithkey:@"Weather"]; NSLog (@"Turn off local notifications"); }}
Uilocalnotification How to use local notifications