iPhone and iPad developed iOS timing local push function through LocalNotification

  • 2020-12-13 19:08:29
  • OfStack

Through UILocalNotification Class of iOS, the timed push function of local app can be realized, even if the current app is in the background closed state.

You can do things like set app badgenum, pop up an alert, play sounds, etc. It's pretty easy to do


UILocalNotification *notification=[[UILocalNotification alloc] init];
 if (notification!=nil) {
  NSDate *now=[NSDate new];
  notification.fireDate=[now dateByAddingTimeInterval:15];
  notification.timeZone=[NSTimeZone defaultTimeZone];
  notification.alertBody=@" Regular push notifications! ";
  notification.soundName = @"default";
  [notification setApplicationIconBadgeNumber:22];
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];
 }


demo is very convenient, and it is quite practical to use in appropriate occasions

Source code links: https: / / github com/andypan1314 / LocalNotificationTest

iOS is set to push local notifications every day at 4pm


UILocalNotification *notification=[[UILocalNotification alloc] init];
 if (notification!=nil) {// Determine if the system supports local notifications 
  notification.fireDate = [NSDate dateWithTimeIntervalSince1970:16*60*60*24];// This time the immediate execution cycle is started 
  notification.repeatInterval=kCFCalendarUnitWeekday;// The cycle of notifications 
  notification.timeZone=[NSTimeZone defaultTimeZone];
  notification.alertBody=@" aww ";// A pop-up message 
  notification.applicationIconBadgeNumber=0; // Small number in the upper right corner of the application 
  notification.soundName= UILocalNotificationDefaultSoundName;// The sound of localized notifications 
//notification.alertAction = NSLocalizedString(@" The beauty! ", nil); // The prompt box button pops up 
  notification.hasAction = NO;
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];
 }

Related articles: