您的位置:首页 > 产品设计 > UI/UE

iOS开发——在特定时间、任意时间做本地推送UILocalNotification

2014-12-30 22:40 337 查看
当需要发送一个本地推送的时候,我们需要为其设置fireTime即发送时间,网上好多示例代码只是简单地将一个类似10秒之后的时间设上去,但我们可能更需要在自定义或用户定义的某个特定的时间发送,其实这也不难,算是OC的知识点了——对常用类之时间类的运用。

首先我们需要一个具体的时间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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: