2016-06-14 27 views
8

在UILocalNotification我們使用NSCalendarUnitMinute像重複.....但我找不到在iOS 10 UserNotification doc ...我如何使用NSCalendarUnitMinute像iOS 10中的重複UserNotification如何在iOS 10 UserNotifications上設置NSCalendarUnitMinute repeatInterval?

這裏是代碼,將在晚上8點半安排本地通知,並在每一分鐘後重復。

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = pickerDate; 
localNotification.alertBody = self.textField.text; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.repeatInterval = NSCalendarUnitMinute; 
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

回答

1

使用UNCalendarNotificationTriggerDateComponents代替NSCalendar

var date = DateComponents() 
date.hour = 8 
date.minute = 30 

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true) 
let content = UNNotificationContent() 
// edit your content 

let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger) 

您設置的日期與DateComponents實例並指定通知應與UNCalendarNotificationTrigger初始化的repeats參數重複。

UNCalendarNotificationTrigger Documentation

剛擡起頭,有在蘋果文檔一個錯字(截至6/13)。如果使用NSDateComponents而不是DateComponents,則需要在dateMatching參數中明確投出date as DateComponents

作爲對您的評論的迴應,我不相信您想要的行爲(更改重複通知的頻率)由UNCalendarNotificationTrigger支持。

+0

我想在明天晚上8:30發佈UserNotification,這將重複每一分鐘,如何在UserNotification中做到這一點。 –

+0

@ S.J我沒有看到API中的任何方法來改變你所要求的通知頻率。我每天只看到一次重複選項。用'UILocalNotification'有可能嗎?你可以做的是在第一次通知的處理程序中,在當前一次通知後的一分鐘內設置一個新的通知。我會繼續尋找一種方法來完成你想要的行爲。 – JAL

+0

您是否閱讀過[UserNotifications文檔](https://developer.apple.com/reference/usernotifications)? – JAL

0

熊與我這是我的第一篇文章。

我們需要1分鐘的重複時間間隔爲我們的應用程序在ios 10中,並作爲一個解決方案組合新老框架。

  1. 調度與舊的重複本地通知:

    UILocalNotification * LN = [[UILocalNotification的alloc] INIT];
    ...
    ln.repeatInterval = kCFCalendarUnitMinute;
    ln.userInfo = @ {@「alarmID」:...}
    ...
    [[UIApplication sharedApplication] scheduleLocalNotification:ln];

這將創建和調度通常重複淋巴結,將顯示爲未通知與UNLegacyNotificationTrigger

我用alarmID連接新老框架。

嘗試:

UNUserNotificationCenter* center = [UNUserNotificationCenter 

currentNotificationCenter]; 
[center getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) { 
    NSLog(@"----------------- PendingNotificationRequests %i -------------- ", (int)requests.count); 
    for (UNNotificationRequest *req in requests) { 
     UNNotificationTrigger *trigger = req.trigger; 
     NSLog(@"trigger %@", trigger); 
    } 
}]; 
  • 響應動作和火災-ING通知,操縱通知中心:
  • 隨着新的框架UNUserNotificationCenter方法: willPresentNotification: didReceiveNotificationResponse:

    1. 我以通常的方式爲這兩個框架做了請求授權和類別註冊。

    希望這有助於

    2

    雖然看起來舊的通知框架有這方面更好的支持,這是唯一的,直到我們認識到,新的更好!

    我有類似的問題,下面是如何舊通知可以與新的如何攜手的示例。

    爲了更安全一點,這是Swift2.3

    // Retrieve interval to be used for repeating the notification 
        private func retrieveRepeatInterval(repeatTemp: RepeatInterval) { 
         switch repeatTemp { 
         case .Never: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month, .Year], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = false 
          } else { 
           repeatIntervalTemp = nil 
          } 
         case .EveryMinute: 
          if #available(iOS 10.0, *) { 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .Minute 
          } 
         case .EveryHour: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Minute], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .Hour 
          } 
         case .EveryDay: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .Day 
          } 
         case .EveryWeek: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Weekday], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .WeekOfYear 
          } 
         case .EveryMonth: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .Month 
          } 
         case .EveryYear: 
          if #available(iOS 10.0, *) { 
           toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month], fromDate: timeTemp!) 
           toDateComponents.second = 0 
           repeatNotification = true 
          } else { 
           repeatIntervalTemp = .Year 
          } 
         } 
        } 
    

    雖然這不是詳盡無遺,但我幾乎涵蓋了從一分鐘到一年的所有內容。

    而且更詳細,

    // RepeatInterval 
    enum RepeatInterval : String, CustomStringConvertible { 
        case Never = "Never" 
        case EveryMinute = "Every Minute" 
        case EveryHour = "Every Hour" 
        case EveryDay = "Every Day" 
        case EveryWeek = "Every Week" 
        case EveryMonth = "Every Month" 
        case EveryYear = "Every Year" 
    
        var description : String { return rawValue } 
    
        static let allValues = [Never, EveryMinute, EveryHour, EveryDay, EveryWeek, EveryMonth, EveryYear] 
    } 
    
    var repeatTemp: RepeatInterval? 
    
    var repeatIntervalTemp: NSCalendarUnit? 
    
    var timeTemp: NSDate? 
    
    var toDateComponents = NSDateComponents() 
    

    由於這個工作對我來說,它或許應該爲你休息。請嘗試讓我知道是否有問題。

    而對於這個問題的確切解決方案,我建議下面。

    (大概是,蘋果並沒有想到這樣的場景,但它可以處理)

    1. 時間表8:30 PM
    2. 當通知被觸發的通知,將其刪除並時間表另一個具有不同標識符的通知,用於上面顯示的NSCalendarUnitMinute等效項。
    3. 瞧,它的工作! (或沒有?)
    +0

    你怎麼知道通知已被觸發,所以安排下一個?我已經看到了使用通知服務擴展的建議,但它們只能用於遠程通知:( – CMash

    +0

    @CMash。你是對的..對錯誤的信息抱歉,提供了。我正在玩這個,並且似乎有它想象但是,看起來我沒有辦法。:(但是,我會說'NSDateComponents',很清楚嗎?如果有人需要使用它。 – Shyam

    相關問題