最近在公司做一个功能 做一个日历 并且集成outlook 根据响应的日程时间显示出邮箱的数据,并且邮箱里面有会议时 ,像闹钟一样提醒用户.以前也没做过相应的功能,感觉压力山大,日历因为项目时间紧,所以找了个第三方的改造一下来适应项目的需求.基本搞定,对于日程的提醒还是第一次接触,在网上搜了许多资料,不是方法太老,就是收不到提醒,最后看了文档添加了一个方法就能收到本地的通知.
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
完整代码
#pragma mark initView
-(void)initView{
if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
NSInteger period = 10.0;
UILocalNotification *notification=[[UILocalNotification alloc] init];
if(notification!=nil){
NSDate *now = [NSDate date];
notification.fireDate = [now dateByAddingTimeInterval:period];
notification.repeatInterval = 0;//循环次数
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = @"该去打豆豆了";
notification.alertAction =NSLocalizedString(@"ddddd", nil);
notification.hasAction = YES;
NSDictionary* infoDic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
notification.userInfo = infoDic;
//发送通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
有点略坑啊 在网上的许多代码 本地通知都没有这段代码 导致 在模拟器上运行没有收到本地通知