The use of notification under iOS
Notification is a very common mechanism of transmitting information in the application programming of smart phone, and it can save resources very well, without consuming resources to check information status (Pooling), the application in iOS is divided into two different kinds of notification, local and remote. The local notification is managed centrally by iOS under Notificationmanager, simply by adding the encapsulated local notification object to the system notification management mechanism queue. The system will fire the local notification at the specified time, and the application will complete the notification process simply by designing a method to handle notification.
The objects that are used by local notification are uilocalnotification,uilocalnotification properties that cover all the content that is required to process notification. The attributes of Uilocalnotification are Firedate, TimeZone, Repeatinterval, Repeatcalendar, Alertbody, Alertaction, HasAction, Alertlaunchimage, Applicationiconbadgenumber, Soundname and UserInfo.
Dispatch of Uilocalnotification
Where Firedate, TimeZone, Repeatinterval, and Repeatcalendar are scheduled for uilocalnotification. Firedate is the exact time of uilocalnotification excitation. TimeZone is whether the uilocalnotification excitation time is changed according to the time zone change, if set to nil, then the uilocalnotification will be excited after a period of time, not an exact moment is excited. Repeatinterval is the time difference between uilocalnotification being repeated excitation, but the time difference is completely based on the calendar unit (nscalendarunit), such as the units that are fired weekly, Nsweekcalendarunit, If not set, the excitation will not be repeated. Repeatcalendar is the calendar that the calendar units used by the uilocalnotification repeat firing need to refer to, and if not set, the system default calendar will be used as the reference calendar.
Uilocalnotification's reminder content
Alertbody, Alertaction, Hasaction, and Alertlaunchimage are when the application is not running, the system processes
Uilocalnotification reminders are required content. Alertbody is a string of real-world reminders (NSString), which is not a realistic reminder if alertbody is not set. Alertaction is also a string of characters (NSString), Alertaction's content will be used as the text on the action button in the reminder, and if not set, the action button in the reminder will appear as "View" relative to the text. Alertlaunchimage is when the user clicks the action button ("View") in the Reminder box and waits for the image to be displayed when the app loads, which replaces the loaded image that was originally set by the app. Hasaction is a Boolean value that controls whether an action button is displayed in a reminder box, and the default is yes.
Other parts of the uilocalnotification
Applicationiconbadgenumber, Soundname and UserInfo will make the uilocalnotification more complete. Applicationiconbadgenumber is the number displayed in the upper-right corner of the app icon, which gives the user a direct view of the Notification the app needs to handle. Soundname is another uilocalnotification used to remind the user that after notification is fired, the sound will be played to remind the user that there is a notification to be processed (when the sound is set, the name of the sound is added to the extension, such as Sound1.mp3), if the soundname is not set, notification is fired will not have a sound playback, except for the application of special-purpose sound, You can also set Soundname to Uilocalnotificationdefaultsoundname to use the system default reminder sound. UserInfo is the nsdictionary that notification uses to pass data.
Registration Uilocalnotification
After the Uilocalnotification object is set up, the app needs to enlist the Uilocalnotification objects that have been set up in the System notification processing queue. The way to register Uilocalnotification * Localnotification is:
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
In some cases, the application may need to directly excite a notification instead of waiting for a period of time to excite, and the application can directly trigger the set-up notification in the following ways:
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
Handling Uilocalnotification
When the Alert Box action button is clicked, the app starts running and can be handled in-(BOOL) Application:didfinishlaunchingwithoptions: This application delegate method. You can load the notification as recently unhandled by:
UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
If the app is running, you can handle Notification by overriding the Method-(void) Application:didreceivelocalnotification: In application delegate. The second argument as a method is the Uilocalnotification object, which simply handles the userinfo the object carries to handle the action of the response.
Cancel Uilocalnotification
You can use the following two ways to cancel a registered notification, the first way you can directly cancel a specified notification, the second way will be the application registered notification together cancel
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
[[UIApplication sharedApplication] cancelAllLocalNotification];
It is generally necessary for the application to run in the background to display a prompt, and the foreground runtime generally does not display a prompt. If you want to display a prompt when the application foreground should be displayed, you can do so by adding the following function to Appdelegate:
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{
Uiapplicationstate state = Application.applicationstate;
NSLog (@ "%@,%d", notification,state);
if (state = = uiapplicationstateactive) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Reminders"
Message:notification.alertBody
Delegate:self
cancelbuttontitle:@ "Close"
otherbuttontitles:@ "OK", nil];
[Alert show];
[Alert release];
}
}
Summarize
The mechanism of local notification is very effective in application development, which can help developers to manage some events that need to happen at specified time, such as the application of alarm class. And because the system unifies the management of the notification, so that the same task can be very simple to be processed, without allowing the application to waste resources to wait for the event to trigger.
Use of iOS uilocalnotification