// -------------------注册本地通知-----------------
// 创建一个本地通知对象
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// 设置本地通知的时间
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 通知内容
localNotification.alertBody = @"起床了!";
// 设置APP打开的按钮
localNotification.alertAction = @"起床";
// 设置应用程序未读消息数
localNotification.applicationIconBadgeNumber = 1;
// 设置播放的声音
localNotification.soundName = @"44th Street Medium.caf";
// 配置通知的信息
localNotification.userInfo = @{@"name":@"朱思明"};
// 设置声音的提示周期
localNotification.repeatInterval = NSCalendarUnitDay;
// 把通知注册到系统里
[application scheduleLocalNotification:localNotification];
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"name:%@",notification.userInfo[@"name"]);
application.applicationIconBadgeNumber = 0;
}
取消通知
- (IBAction)button:(id)sender
{
// 获取已在系统里面注册的通知对象
NSArray *locals = [[UIApplication sharedApplication] scheduledLocalNotifications];
if (locals.count <= 0)
{
return;
}
for (UILocalNotification *local in locals)
{
if ([local.userInfo[@"name"] isEqualToString:@"朱思明"])
{
// 取消通知
[[UIApplication sharedApplication] cancelLocalNotification:local];
}
}
}