关键是画线的两句代码:(在app启动时写这个代码也行,ios8需要注册本地通知)
- (void)viewDidLoad {
[super viewDidLoad];
app=[UIApplication sharedApplication];
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changed:(id)sender {
UISwitch *sw=(UISwitch *)sender;
if (sw.on) {
UILocalNotification *notification=[[UILocalNotification alloc]init];
notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.repeatInterval=kCFCalendarUnitMinute;
notification.soundName=@"gu.mp3";
notification.alertAction=@"打开";
notification.hasAction=YES;
notification.alertLaunchImage=@"logo.png";
notification.alertBody=@"你好,你收到一条新的消息";
notification.applicationIconBadgeNumber=1;
NSDictionary *info=@{@"java.org":@"key"};
notification.userInfo=info;
[app scheduleLocalNotification:notification];
}
else
{
NSArray *localArray=[app scheduledLocalNotifications];
if (localArray) {
for (UILocalNotification *noti in localArray) {
NSDictionary *dict=noti.userInfo;
if (dict) {
NSString *key=[dict objectForKey:@"key"];
if ([key isEqualToString:@"java.org"]) {
[app cancelLocalNotification:noti];
}
}
}
}
}
}