码迷,mamicode.com
首页 > 其他好文 > 详细

本地通知的实现

时间:2016-01-03 00:41:44      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

 
- (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;
   
}
 
 
 
 
 

本地通知的实现

标签:

原文地址:http://www.cnblogs.com/benpaobadaniu/p/5095515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!