There can be only one application active at any time in most mobile apps, and if other apps are interested in what some users are doing at the moment, the notification mechanism can tell the user what's happening at the moment. The notification mechanism in iOS is also called a messaging mechanism, which consists of two categories: a local notification, and a push notification, also known as a remote notification. The two notifications are consistent in iOS, can be told by a banner or pop-up alert, and clicking Notifications opens the application, but the implementation principle is completely different. I'm going to go see you today. How to implement both mechanisms in iOS, and the content in the notification hubs is supplemented later in the article to avoid confusion between the two concepts by beginners.
Simply a demo of the local push notification can be based on this reference, this method is used after 8.0, so you need to determine the version of the system.
1. Registering a local notification method
+ (void) Registerlocalnotification: (Nsinteger) alerttime {uilocalnotification*notification =[[Uilocalnotification alloc] init]; //set the time to trigger notificationsNSDate *firedate =[NSDate Datewithtimeintervalsincenow:alerttime]; NSLog (@"firedate=%@", firedate); Notification.firedate=firedate; //Time zone excitation time changed based on time zone changeNotification.timezone =[Nstimezone Defaulttimezone]; //set the recurrence intervalNotification.repeatinterval =Kcfcalendarunitsecond; //text that notifies the content background runtime promptNotification.alertbody =@"It's time to get up ..."; Notification.applicationiconbadgenumber=1; //sounds that are played when notifications are triggeredNotification.soundname =Uilocalnotificationdefaultsoundname; //Notification ParametersNsdictionary *userdict = [Nsdictionary dictionarywithobject:@"start learning about iOS development"Forkey:@"Key"]; Notification.userinfo=userdict; //after iOS8, you need to add this registration to be authorized if([[UIApplication sharedapplication] Respondstoselector: @selector (registerusernotificationsettings:)]) { Uiusernotificationtype 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];}
Examples of cancellation of local notifications:
//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 *info =Userinfo[key]; //If you find a notification that you want to cancel, cancel if(Info! =Nil) {[[UIApplication sharedapplication] cancellocalnotification:notification]; Break; } } }}
Methods for notifying callbacks:
//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 *notmess = [Notification.userinfo objectforkey:@"Key"]; Uialertview*alert = [[Uialertview alloc] Initwithtitle:@"local notifications ...."message:notmess 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; //Push once, do not need to display push, you can cancel push, will add the push purge[Homeviewcontroller Cancellocalnotificationwithkey:@"Key"];}
IOS Local push