Local notifications are triggered by a local app, which is a form of notification based on time behavior, such as alarm timing, to-do reminders, or when an app is not used for a period of time and is usually prompted to use the app, etc. are local notifications. Creating a local notification is usually divided into the following steps:
- Create a uilocalnotification.
- Sets the time firedate for processing notifications.
- Configure the contents of the Notification: notification body, notification sound, icon number, and so on.
- Configure custom data parameter userinfo for notification delivery (this step is optional).
- Call notifications, you can use schedulelocalnotification: schedule A notification as scheduled, or use Presentlocalnotificationnow to call notifications immediately.
The following is an example of a program update after the user has not been used for a long time to do a simple understanding of local notifications. In this process is not involved in too much interface operation, all the logic is in the appdelegate: if there is no registration notification after entering the application, you need to register the notification request to allow the user to notify; Once the registration method is called, the application will be invoked regardless of whether the user chooses to allow notifications -( void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings *) Notificationsettings Proxy method, in this method according to the user's choice: If the Allow notification will follow the previous steps to create a notification and after a certain time to execute.
Appdelegate.m
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {// Override point for customization after application launch. _window=[[uiwindow Alloc]initwithframe:[uiscreen Mainscreen].bounds]; _window.backgroundcolor =[uicolor colorwithred:249/255.0 green:249/255.0 blue:249/255.0 alpha:1]; Set the global navigation bar style and color [[Uinavigationbar appearance] Setbartintcolor:[uicolor colorwithred:23/255.0 green:180/255.0 blue:237 /255.0 Alpha:1]]; [[Uinavigationbar appearance] setbarstyle:uibarstyleblack]; Viewcontroller *maincontroller=[[viewcontroller Alloc]init]; _window.rootviewcontroller=maincontroller; [_window makekeyandvisible]; if ([[UIApplication sharedapplication]currentusernotificationsettings].types!=uiusernotificationtypenone) {[Self Addlocation]; }else{[[UIApplication sharedapplication]registerusernotificationsettings:[uiusernotificationsettings SettingsFor Types:uiusernotificAtiontypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil]]; } return YES; -(void) addlocation{//define local notification object Uilocalnotification *notification=[[uilocalnotification Alloc]init]; Set the call time notification.firedate=[nsdate datewithtimeintervalsincenow:10.0];//notification trigger time, after 10s notification.repeatinterval=4;//notification repetitions//notification.repeatcalendar=[nscalendar currentcalendar];//the current calendar, It is best to set the time zone and other information before using to automatically synchronize the Times//set the notification properties [email protected] "recently added a lot of interesting features, whether to experience immediately?" "; Notifies the principal notification.applicationiconbadgenumber=1;//the number of messages displayed in the upper-right corner of the application icon [email protected] "open app"; The sliding action hint of the standby interface [email protected] "Default";//click the notification to open the app when the start picture, here Use the program to start the picture//notification.soundname= uilocalnotificationdefaultsoundname;//The sound that is played when the notification is received, the default message sound [email protected] "MSG.CAF";//Notification sound (requires the real machine to hear sound)//settings User information [email protected]{@ "id": @1,@ "User": @ "Kenshin Cui"};//bind to other additional information on the notification//Invoke notification [[UIApplication SHAREDAPPL Ication] Schedulelocalnotification:nOtification]; }-(void) removenotification{[[UIApplication sharedapplication] cancelalllocalnotifications];} -(void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings * ) notificationsettings{if (notificationsettings.types!= uiusernotificationtypenone) {[Self addLocation]; }}
iOS Development-Local notifications