7

直到iOS 9我們寫local notifications這樣如何設置用戶通知重複頻率

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]; 

和本地通知我們repeatInterval,現在WWDC2016蘋果宣佈User Notification包含

  • UNTimeIntervalNotificationTrigger。
  • UNCalendarNotificationTrigger。

    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES]; 
    

上面的代碼將每分鐘後觸發通知。但不能設置日期。

NSDateComponents* date = [[NSDateComponents alloc] init]; 
date.hour = 8; 
date.minute = 30; 

UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES]; 

上面的日期代碼可以設置,重複將在明天8點半後觸發,不會在一分鐘後。

在iOS 10 User Notification如何設置重複頻率的日期時間,就像我們可以在UILocalNotification中設置一樣?

我想在下午8:30安排明天User Notification,並保持每分鐘就像我在上面指定的代碼就local notification

+0

大家好,我有同樣的問題。請讓我們知道是否有人已經實現了此功能? **供參考:**問題是「如何爲** iOS 10 **中的本地通知設置像日常,每月,每分鐘等重複間隔」。我在等。謝謝。 –

+1

找到解決上述問題的方法嗎? –

+0

也非常相關:[用戶通知3天然後重複每一天/小時 - iOS 10](http://stackoverflow.com/q/38380783/2415822) – JAL

回答

0

在iOS設備上10,你可以用這樣的重複後:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate]; 
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; 

它與iOS 9代碼具有相同的效果。重複你只需要使用你想要重複的組件。

1

通過@Ramon洛斯的啓發,我使用下面的代碼設置間隔fireDate一起

switch (interval) { 
    case NSCalendarUnitMinute: { 
     unitFlags = NSCalendarUnitSecond; 
     break; 
    } 
    case NSCalendarUnitHour: { 
     unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond; 
     break; 
    } 
    case NSCalendarUnitDay: { 
     unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
     break; 
    } 
    case NSCalendarUnitWeekOfYear: { 
     unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
     break; 
    } 
    case NSCalendarUnitMonth:{ 
     unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
    } 
    case NSCalendarUnitYear:{ 
     unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
    } 
    default: 
     unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
     break; 
} 
NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate]; 
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:interval != 0];