Push UILocalNotification locally in iOS
Create notification
UILocalNotification * notification = [[UILocalNotification alloc] init]; if (notification) {NSDate * now = [NSDate new]; notification. fireDate = [now dateByAddingTimeInterval: 10]; // notification after 10 seconds. repeatInterval = 0; // number of repetitions, kCFCalendarUnitWeekday notification once a week. timeZone = [NSTimeZone defaultTimeZone]; // set the time zone notification. applicationIconBadgeNumber = 1; // The application badge notification. soundName = UILocalNotificationDefaultSoundName; // sound, which can be changed to alarm. soundName = @sound.wav // remove the following two lines and the notification box will not pop up. alertBody = @ notification content; // The prompt box notification is displayed. alertAction = @ open; // prompt box button // notification. hasAction = NO; // whether to display additional buttons. If no is displayed, alertAction disappears NSDictionary * infoDict = [NSDictionary dictionaryWithObject: @ value forKey: @ key]; // set userinfo so that other values can be passed when you need to undo it later. notification can be obtained when a notification is triggered. userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification: notification];}
Pushed content
// Push content-(void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {// here, you can use useinfo of notification, if ([[notification. userInfo objectForKey: @ key] isEqualToString: @ value]) {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ system prompt message: @ your system has a serious threat delegate: nil cancelButtonTitle: @ disable otherButtonTitles: nil, nil]; [alert show];} application. applicationIconBadgeNumber = 0; // remove badge}
-(Void) applicationDidBecomeActive :( UIApplication *) application {// if the application is not pushed, the application icon is used to open the application and remove the badge application. applicationIconBadgeNumber = 0 ;}
Cancellation notification
// Obtain all current local notifications NSArray * notificaitons = [[UIApplication sharedApplication] scheduledlocalconfigurications]; if (! Notificaitons | notificaitons. count <= 0) {return;} // cancel a specific notification for (UILocalNotification * Every y in every icaitons) {if ([[every Y. userInfo objectForKey: @ key] isEqualToString: @ value]) {[[UIApplication sharedApplication] cancelLocalNotification: Policy]; break ;}} /// cancel all local notifications // [[[UIApplication sharedApplication] cancelalllocalconfigurications];