UILocalNotification

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    // 触发通知的时间
    NSDate *now = [formatter dateFromString:@"15:04:00"];
    notification.fireDate = now;
 // 触发通知的时间
    NSDate *currDate = [NSDate date];
    NSTimeInterval  interval = 30*60; // 半个小时
    NSDate *after30minDate = [currDate initWithTimeIntervalSinceNow:+interval];
    NSString *currTime = [formatter stringFromDate:after30minDate];
    
    NSDate *notificationTme = [formatter dateFromString:currTime];
    notification.fireDate = notificationTme;
    
    notification.fireDate = [currDate dateByAddingTimeInterval:9];

// 设置时区,默认即可
    notification.timeZone=[NSTimeZone defaultTimeZone];
    // 通知重复提示的单位,可以是天、周、月
    notification.repeatInterval = NSDayCalendarUnit;
    // 通知内容
    notification.alertBody = @"这是一个新的通知";
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    // 设置IconBadgeNumber
    notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
    // 设置本地通知发送的消息,这个消息可以接受
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"setNotificationForEveryDay", @"key", nil];
    notification.userInfo = infoDict;
    //执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
self.window
= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; } // 应用收到通知时,会调到下面的回调函数 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LocalNotification" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; //这里可以接受到本地通知中心发送的消息 NSDictionary* dic = notification.userInfo; // 图标上的数字减1 application.applicationIconBadgeNumber -= 1; NSLog(@"user info = %@",[dic objectForKey:@"key"]); }
原文地址:https://www.cnblogs.com/joesen/p/3586513.html