Local notifications are notifications Based on Time behaviors, such as small applications related to calendar or todo lists. In addition, if the app is executed in the background, iOS allows it to run within a limited period of time, and it will also find local notifications useful. For example, an application runs in the background and obtains a message from the application server. When a message arrives, for example, a prompt message for downloading an updated version, a local notification mechanism is used to notify the user.
Local notification is an instance of UILocalNotification, which has three attributes:
Scheduled time, a time period, used to specify the date and time when the iOS system sends notifications;
Notification type, notification type, including warning information, action button title, badge (digital flag) on the app icon, and playing sound;
Custom Data. Local notifications can contain local data of the dictionary type.
The maximum number of local notifications allowed by iOS is 64. Local notifications that exceed the limit will be ignored by iOS.
It is very easy to write a simple scheduled reminder, for example:
The example is easy to write. After the application is started, a scheduled notification will be sent, which will be started 10 seconds later. Press the Home Key to exit. A prompt will be displayed later. It is invalid if the application does not exit.
The Code is as follows:
UILocalNotification * notification = [[UILocalNotification alloc] init];
If (notification! = Nil ){
NSLog (@ "> support local notification ");
NSDate * now = [NSDate new];
Notification. fireDate = [now addTimeInterval: 10];
Notification. timeZone = [NSTimeZone defaultTimeZone];
Notification. alertBody = @ "it's time to eat dinner! ";
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
For more detailed code, see the official document Scheduling, Registering, and Handling communications, which can be set to sound, such as user-defined data.
Set more local notification information:
Set the number on the icon.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {
// Override point for customization after application launch.
/////////////
Application. applicationIconBadgeNumber = 0;
// Add the view controller's view to the window and display.
[Self. window addSubview: viewController. view];
[Self. window makeKeyAndVisible];
Return YES;
}
Add notification time, notification type, cancel notification
# Pragma mark-
# Pragma mark onChageValue
-(IBAction) onChangeValue :( id) sender
{
UISwitch * swititch = (UISwitch *) sender;
If (switch1.on ){
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDate * now1 = [NSDate date];
Notification. timeZone = [NSTimeZone defaultTimeZone];
Notification. repeatInterval = NSDayCalendarUnit;
Notification. applicationIconBadgeNumber = 1;
Notification. alertAction = NSLocalizedString (@ "", nil );
Switch (switch1.tag ){
Case 0:
{
Notification. fireDate = [now1 dateByAddingTimeInterval: 10];
Notification. alertBody = self. myLable1.text;
}
Break;
Case 1:
{
Notification. fireDate = [now1 dateByAddingTimeInterval: 20];
Notification. alertBody = self. myLable2.text;
}
Break;
Case 2:
{
Notification. fireDate = [now1 dateByAddingTimeInterval: 30];
Notification. alertBody = self. myLable3.text;
}
Break;
Default:
Break;
}
[Notification setSoundName: uilocalnotificationdefasoundname];
NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat: @ "% d", switch1.tag], @ "key1", nil];
[Notification setUserInfo: dict];
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
} Else {
NSArray * myArray = [[UIApplication sharedApplication] scheduledlocalconfigurications];
For (int I = 0; I <[myArray count]; I ++ ){
UILocalNotification * myUILocalNotification = [myArray objectAtIndex: I];
If ([[[myUILocalNotification userInfo] objectForKey: @ "key1"] intValue] = switch1.tag ){
[[UIApplication sharedApplication] cancelLocalNotification: myUILocalNotification];
}
}
}
}
From Evolution