本地通知的实现

 
- (IBAction)openLocal:(id)sender {
   
    UILocalNotification *ln = [[UILocalNotification alloc] init];
    ln.alertAction = @"赶集赶紧";
    ln.alertBody = @"我是本地通知";
    ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    ln.timeZone = [NSTimeZone defaultTimeZone];
    ln.repeatInterval = kCFCalendarUnitSecond;
    ln.applicationIconBadgeNumber = 2;
    ln.userInfo = @{@"key":@"go to study iOS"};
   
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else {
    }
   
    [[UIApplication sharedApplication] scheduleLocalNotification:ln];
   
}
- (IBAction)removeLocal:(id)sender {
   
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    NSLog(@"%@" , [UIApplication sharedApplication].scheduledLocalNotifications);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"我收到本地通知了");
   
    NSString *message = notification.userInfo[@"key"];
   
    NSLog(@"%@" , message);
   
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge --;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
   
}
 
 
 
 
 

你可能感兴趣的:(本地通知的实现)