您的位置:首页 > 其它

本地通知的实现

2016-01-02 23:14 260 查看
- (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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: