2013-04-02 35 views
2

因此,我有一個應用程序,其中包括重複間隔本地通知,我想添加一個功能,將在睡眠時間暫停通知。根據時間在後臺激活本地通知

到目前爲止,我已經創建了兩個datepickers爲用戶spicify他們想什麼時候停止重複間隔時間和什麼時間自動開始重新啓動。我爲他們添加了一個uiswitch來激活睡眠模式或忽略該功能。

現在,我將如何讓我的主uipickerview - (他們從那裏選擇通知) - 如果它正在打開,那麼它將暫停通知,然後暫停來自我的第一個日期選擇器的通知並重新提交來自第二個日期選擇器的通知?

我已經設置我的datepickers和我uiswitch,但不知道如何與我的uipickerview實現它..它應該是DidSelectRow下的方法?或者在appdelegate中使用方法(如DidEnterBackground)?

,如果您需要了解更多信息或代碼來理解的想法,幫我請詢問。謝謝。

加上:

這裏是我的代碼,我已爲準備日期選擇器,但是,我只是缺少連接到它正確地添加到我的選擇器視圖。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
dateFormatter.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter.timeStyle=NSDateFormatterShortStyle; 
dateFormatter.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date]; 
NSLog(@"Start time is %@",dateTimeString); 


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init]; 
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone]; 
dateFormatter2.timeStyle=NSDateFormatterShortStyle; 
dateFormatter2.dateStyle=NSDateFormatterShortStyle; 
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date]; 
NSLog(@"End time is %@",dateTimeString2); 

我也有這個代碼的時間之間進行比較:

if ([[NSDate date] isEqualToDate:startTime.date]) { 
NSLog(@"currentDate is equal to startTime"); 
} 

if ([[NSDate date] isEqualToDate:endTime.date]) { 
NSLog(@"currentDate is equal to endTime"); 
} 
+0

準確無法得到您的要求?您想停止本地通知.. – Madhu

+0

http://stackoverflow.com/a/11139002/1059705使用此命令可以使用LocalNotificationID計劃LocalNotification,並且您可以使用LocalNotificationID取消任何特定的localNotification – Bala

回答

2

發佈兩個特定日期之間的局部重複通知/次iOS不支持。此外,與基於地理位置的通知不同,應用程序在未處於前臺時不會觸發已觸發的通知。所以我們需要通過在用戶沒有睡着的時候創建許多單獨的通知來解決這個問題。

按照以下步驟創建一個基本的解決方案:

  1. 你的控件連接到您的視圖控制器的頭IBOutlets:

    SomeViewController.h:

    @interface SomeViewController : UIViewController 
    
    @property (weak, nonatomic) IBOutlet UISwitch *sleepToggleSwitch; 
    @property (weak, nonatomic) IBOutlet UIDatePicker *notificationIgnoreStartTime; 
    @property (weak, nonatomic) IBOutlet UIDatePicker *notificationIgnoreEndTime; 
    @property (weak, nonatomic) IBOutlet UIPickerView *notificationTypePickerView; 
    
    @end 
    
  2. 創建IBAction方法並連接您的每個控件(兩個UIDatePickers,一個UISwitch和一個UIPickerV在你的視圖控制器的實現文件中。每種方法都應該調用私有方法startUserOptionInteractionTimer

    SomeViewController.m:

    - (IBAction)noNotificationPeriodStartDateChanged:(id)sender 
    { 
        [self startUserOptionInteractionTimer]; 
    } 
    
    - (IBAction)noNotificationPeriodEndDateChanged:(id)sender 
    { 
        [self startUserOptionInteractionTimer]; 
    } 
    
    - (IBAction)sleepToggleSwitchToggled:(id)sender 
    { 
        [self startUserOptionInteractionTimer]; 
    } 
    
    - (IBAction)notificationTypeChanged:(id)sender 
    { 
        [self startUserOptionInteractionTimer]; 
    } 
    
  3. startUserOptionInteractionTimer私有方法,我們(重新)啓動一個NSTimer。我們在這裏使用計時器,以便如果用戶更改日期或快速切換交換機 - 他們可能會這樣做 - 您不會拆卸並快速連續設置通知。 (NSTimer屬性userOptionInteractionTimer應在實現文件的接口延續中聲明)。

    SomeViewController.m:

    - (void)startUserOptionInteractionTimer 
    { 
        // Remove any existing timer 
        [self.userOptionInteractionTimer invalidate]; 
        self.userOptionInteractionTimer = [NSTimer scheduledTimerWithTimeInterval:4.f 
                         target:self 
                        selector:@selector(setupNotifications) 
                        userInfo:nil 
                         repeats:NO]; 
    } 
    
  4. 創建推倒預先存在的通知,並設置了新的另一個私有方法。

    在此處設置通知取決於您想要通知用戶的時間和頻率。假設您希望每隔一小時通知用戶並且用戶啓用了睡眠功能,那麼您將爲每天重複的每個小時設置14-18個通知(取決於用戶睡眠的時間)。

    SomeViewController.m:

    - (void)setupNotifications 
    { 
        [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    
        // Read the notification type from the notification type picker view 
        NSInteger row = [self.notificationTypePickerView selectedRowInComponent:0]; // Assumes there is only one component in the picker view. 
        NSString *notificationType = [self.notificationList objectAtIndex:row]; // Where notificationList is the array storing the list of notification strings that appear in the picker view. 
    
        // If the user has turned the sleep feature on (via the UISwitch): 
        if (self.sleepToggleSwitch.on) { 
         // Set the first notification to start after the user selected 'noNotificationPeriodEndDate' and repeat daily. 
         // Add 1 hour to the notification date 
         // Do while the notification date < the user selected 'noNotificationPeriodStartDate' ... 
         //  Create the notification and set to repeat daily 
         //  Add 1 hour to the notification date 
         // Loop 
        } else { 
         // Set up 24 repeating daily notifications each one hour apart. 
        } 
    } 
    

    請記住,一個單一的應用程序只能創建最多64個通知(與重複通知作爲一個計數),所以,如果你想通知開火不同在幾天或幾周的時間內,你可能不得不重新考慮一下你的設計。

  5. 負載和用戶選擇的喜好在存儲NSUserDefaults的:

    SomeViewController.m:

    - (void)viewDidLoad 
    { 
        [super viewDidLoad]; 
    
        // Load the user defaults 
        NSDate *sleepStartDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"SleepStartDate"]; 
        self.notificationIgnoreStartTime.date = sleepStartDate ? sleepStartDate : [NSDate date]; 
        NSDate *sleepEndDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"SleepEndDate"]; 
        self.notificationIgnoreEndTime.date = sleepEndDate ? sleepEndDate : [NSDate date]; 
        self.sleepToggleSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"SleepEnabled"]; 
    
        // Watch for when the app leaves the foreground 
        [[NSNotificationCenter defaultCenter] addObserver:self 
                  selector:@selector(applicationWillResignActive) 
                   name:UIApplicationWillResignActiveNotification object:nil]; 
    } 
    
    - (void)applicationWillResignActive 
    { 
        // If the timer is still waiting, fire it so the notifications are setup correctly before the app enters the background. 
        if (self.userOptionInteractionTimer.isValid) 
         [self.userOptionInteractionTimer fire]; 
    
        // Store the user's selections in NSUserDefaults 
        [[NSUserDefaults standardUserDefaults] setObject:self.notificationIgnoreStartTime.date forKey:@"SleepStartDate"]; 
        [[NSUserDefaults standardUserDefaults] setObject:self.notificationIgnoreEndTime.date forKey:@"SleepEndDate"]; 
        [[NSUserDefaults standardUserDefaults] setBool:self.sleepToggleSwitch.on forKey:@"SleepEnabled"]; 
    } 
    

    此外,通知上面,如果應用程序即將進入後臺(即離開前臺),並且定時器仍在滴答滴答,我們強制它啓動,以便在操作系統殺死定時器之前設置通知。

  6. 記住爲所有日期選擇器視圖和所有IBActions以及代表視圖的代表和代表和數據源連接代理IBActions。還請記住設置代理和數據源方法,以便填充選擇器視圖。

  7. 就是這樣!

上述設計將確保通知在正確的時間被觸發,無論應用程序是在前臺,後臺還是終止。 (但是,如果應用程序在收到通知時位於前臺,用戶將不會收到通知,而是調用appDelegate上的application:didReceiveLocalNotification:)。

顯然上面的代碼不是「執行就緒」,但你應該能夠填補空白。

1

UISwitch設置在plist或隨處進行檢查,也許激活和停用選取器NSUserDefaults屬性(userInteractionEnabled等等。)。

還與IBAction梅索德爲ValueChanged連接UISwitch能夠隨時切換狀態。

而且使用DidSelectRowAtIndexPath:梅索德的UIPickerView只是爲了更新時間(也將它們保存在一個plistNSUserDefaults如果你需要他們在多個地方。
或者你使用的屬性類屬性(靜態)^^

對於automaticaly檢查我只想用運行circulary 60秒AppDelegate一個NSTimer和火災,檢查自身的事件,如果它能夠繼續或恢復,或者如果你想火它已經在這裏檢查(可擴展到得到肯定你這些事件在一分鐘內沒有任何地方發生,但在00秒)
檢查你是否可以上網重新與否可能看起來像這樣爲國家

startSilentTimePicker:晚上10:30
stopSilentTimePicker:上午7:15
activitySwitch:上

// filtered date by only time 
NSDateComponents *startComps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[startSilentTimePicker date]]; 
NSDateComponents *endComps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[stopSilentTimePicker date]]; 
NSDateComponents *currentComps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:[NSDate date]]; 

if([[startComps date] laterDate:[currentComps date]] == [currentComps date] || 
[[endComps date] earlierDate:[currentComps date]] == [currentComps date]) 
{ 
    // set the value, that tells you beeing in silent time, set a return from the methode or invert to fire notification here 
} 

沒有我的代碼測試它actualy,但我正在使用類似的東西。如果你失去了一些東西,所以告訴請,也許我可以完成;)