[Cpp]
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat: @ "HH: mm: ss"];
// Time when the notification is triggered
NSDate * now = [formatter dateFromString: @ "15:00:00"];
Notification. fireDate = now;
// Time Zone
Notification. timeZone = [NSTimeZone defaultTimeZone];
// The unit of the duplicate notification, which can be day, week, or month
Notification. repeatInterval = NSDayCalendarUnit;
// Notification content
Notification. alertBody = @ "this is a new notification ";
// The Playing sound when the notification is triggered
Notification. soundName = uilocalnotificationdefasoundname;
// Execute notification Registration
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
The above code implements such a scenario: Some Todo and alarm clock applications have the function of notifying users, using the local notification UILocalNotification in iOS, some applications will prompt users to return to the application at a fixed time every day, every week, or every month, and also use local notifications. The above code snippet will bring up a notification prompt at three o'clock P.M. every day.
To include parameter information in the notification, use the following method:
[Cpp]
NSDictionary * dic = [NSDictionary dictionaryWithObject: @ "name" forKey: @ "key"];
Notification. userInfo = dic;
If the software is running, you can use the callback method in AppDelegate to obtain and process the parameter information:
[Cpp]
-(Void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification
{
If (notification ){
NSDictionary * userInfo = notification. userInfo;
NSString * obj = [userInfo objectForKey: @ "key"];
NSLog (@ "% @", obj );
}
}
In addition, you can cancel the registration of local notifications in two ways, one is to cancel the specified notification, and the other is to cancel all registration notifications:
[Cpp]
[[UIApplication sharedApplication] cancelLocalNotification: localNotification];
[[UIApplication sharedApplication] cancelAllLocalNotification];
The above briefly introduces the use of UILocalNotification. You are welcome to correct and add it!