SoFunction
Updated on 2025-04-12

iOS local push simple implementation code

This article breaks down and introduces three steps to push iOS local code for your reference. The specific content is as follows

first step:Create a local push

// Create a local pushUILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
//After setting 10 secondsNSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
if (notification != nil) {
 // Set push time  = pushDate;
 // Set the time zone  = [NSTimeZone defaultTimeZone];
 // Set the repetition interval  = kCFCalendarUnitDay;
 // Push sound  = UILocalNotificationDefaultSoundName;
 // Push content  = @"Push content";
 //The number displayed in the red circle on the icon  = 1;
 //Set userinfo to be used when it needs to be canceled later NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
  = info;
 //Add push to UIApplication UIApplication *app = [UIApplication sharedApplication];
 [app scheduleLocalNotification:notification]; 
 
}

Step 2:Receive local push

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message: delegate:nil cancelButtonTitle:@"Sure" otherButtonTitles:nil];
 [alert show];
 // The number on the icon is reduced by 1  -= 1;
}

Step 3:Unlock local push

// Obtain UIApplicationUIApplication *app = [UIApplication sharedApplication];
//Get local push arrayNSArray *localArray = [app scheduledLocalNotifications];
//Declare the local notification objectUILocalNotification *localNotification;
if (localArray) {
 for (UILocalNotification *noti in localArray) {
  NSDictionary *dict = ;
  if (dict) {
   NSString *inKey = [dict objectForKey:@"key"];
   if ([inKey isEqualToString:@"Relevant key value"]) {
    if (localNotification){
     [localNotification release];
     localNotification = nil;
    }
    localNotification = [noti retain];
    break;
   }
  }
 }
 
 //Judge whether to find a push of the same key that already exists if (!localNotification) {
  //There is no initialization  localNotification = [[UILocalNotification alloc] init];
 }
 
 if (localNotification) {
  //No push Cancel push  [app cancelLocalNotification:localNotification];
  [localNotification release];
  return;
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.