iOS开发——在特定时间、任意时间做本地推送UILocalNotification
2014-12-30 22:40
337 查看
当需要发送一个本地推送的时候,我们需要为其设置fireTime即发送时间,网上好多示例代码只是简单地将一个类似10秒之后的时间设上去,但我们可能更需要在自定义或用户定义的某个特定的时间发送,其实这也不难,算是OC的知识点了——对常用类之时间类的运用。
首先我们需要一个具体的时间Date,我们就根据这个时间来将其拆分。这个时间通常来自用户设定的时间。
其次,拆分时间需要的两个非常重要的类:NSCalender类和NSDateComponent类。初始化这两个变量,为NSDateComponent类指定Units。
有了component对象,我们就可以自由为其设定时间了。比如这里我希望七点提醒,就为component的hour对象赋值7即可。然后就能从component得到一个Date对象。
这样就实现了设置特定时间的功能。接下来就属于本地通知的知识了。
声明一个UILocalNotification对象(注意做错误判断),然后为其设置各种属性并且调用应用代理将这个通知发出去就好了。
最后别忘了在AppDelegate中launch一下。
附上自己应用中得完整代码供参考纠错 - -
首先我们需要一个具体的时间Date,我们就根据这个时间来将其拆分。这个时间通常来自用户设定的时间。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; NSDate *testDate = [formatter dateFromString:model.testTime]; NSAssert(testDate != nil, @"testDate = nil");
其次,拆分时间需要的两个非常重要的类:NSCalender类和NSDateComponent类。初始化这两个变量,为NSDateComponent类指定Units。
//to set the fire date NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComponents = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:testDate];
有了component对象,我们就可以自由为其设定时间了。比如这里我希望七点提醒,就为component的hour对象赋值7即可。然后就能从component得到一个Date对象。
[dateComponents setHour:7]; NSDate *fireDate = [calender dateFromComponents:dateComponents]; NSLog(@"fire date = %@", fireDate);
这样就实现了设置特定时间的功能。接下来就属于本地通知的知识了。
声明一个UILocalNotification对象(注意做错误判断),然后为其设置各种属性并且调用应用代理将这个通知发出去就好了。
UILocalNotification *noti = [[UILocalNotification alloc] init]; if (noti == nil) { return; } [noti setTimeZone:[NSTimeZone defaultTimeZone]]; noti.fireDate = fireDate; noti.repeatInterval = 0; noti.alertBody = [NSString stringWithFormat:@"%@考试今天%d点就要开始了,地点是%@,你准备好了吗?", model.testName, dateComponents.hour, model.testLocation]; noti.alertAction = @"View"; noti.soundName = UILocalNotificationDefaultSoundName; noti.applicationIconBadgeNumber += 1; noti.userInfo = @{@"Kind" : @"testBeginLocalNotification"}; [[UIApplication sharedApplication] scheduleLocalNotification:noti];
最后别忘了在AppDelegate中launch一下。
//-----AppDelegate的didFinishLaunchingWithOptions方法中------- //reset the application icon badge number application.applicationIconBadgeNumber = 0; // Handle launching from a notification UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { NSLog(@"Recieved Notification %@",localNotif); }
附上自己应用中得完整代码供参考纠错 - -
- (void)setUpLocalNotificationWithModel:(TestModel *)model {
if (model.shouldRemind == NO) {
return;
}
UILocalNotification *noti = [[UILocalNotification alloc] init];
if (noti == nil) {
return;
}
[noti setTimeZone:[NSTimeZone defaultTimeZone]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; NSDate *testDate = [formatter dateFromString:model.testTime]; NSAssert(testDate != nil, @"testDate = nil");
//to set the fire date NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; NSDateComponents *dateComponents = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:testDate];
[dateComponents setHour:7]; NSDate *fireDate = [calender dateFromComponents:dateComponents]; NSLog(@"fire date = %@", fireDate);
noti.fireDate = fireDate;
noti.repeatInterval = 0;
//to get the hour
dateComponents = [calender components:NSCalendarUnitHour fromDate:testDate];
NSLog(@"test date hour = %d", dateComponents.hour);
noti.alertBody = [NSString stringWithFormat:@"%@考试今天%d点就要开始了,地点是%@,你准备好了吗?", model.testName, dateComponents.hour, model.testLocation];
NSLog(@"tip: %@", noti.alertBody);
noti.alertAction = @"View";
noti.soundName = UILocalNotificationDefaultSoundName;
NSLog(@"notification application icon badge number = %d", noti.applicationIconBadgeNumber);
noti.applicationIconBadgeNumber += 1;
noti.userInfo = @{@"Kind" : @"testBeginLocalNotification"};
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
}
相关文章推荐
- 本地通知和推送通知 - iOS开发
- iOS开发推送本地通知
- iOS开发消息推送机制——本地推送。
- iPhone/iPad开发通过LocalNotification实现iOS定时本地推送功能
- iOS开发学习专题-基础知识(五) NSDate时间 NSUserDefaults本地存储 NSNotification系统通知的详细使用方式
- IOS开发中本地消息推送方法
- IOS开发笔记5 -- 本地推送
- ios 开发之本地推送
- IOS开发—本地推送
- (iPhone/iPad开发)通过LocalNotification实现iOS定时本地推送功能
- IOS开发中本地消息推送方法
- iOS开发:本地通知(推送)
- ios开发之-本地推送通知
- iOS开发本地通知(每天定点发送消息推送)
- iOS开发——本地通知,本地推送(UILocalNotification)
- iOS项目开发实战——实现苹果本地消息通知推送服务
- IOS开发中本地消息推送方法
- IOS iOS本地推送---常用开发中,通过消息传递信息
- IOS iOS本地推送---常用开发中,通过消息传递信息
- ios开发之-本地推送通知