iOS開發——在特定時間、任意時間做本地推送UILocalNotification

來源:互聯網
上載者:User

當需要發送一個本地推送的時候,我們需要為其設定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];}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.