How to Set repeat interval as well as fire date in UNNotificationRequest

自作多情 提交于 2021-02-07 18:00:25

问题


Previously I am using UILocalNotification for reminder purpose in my application.

But as above API is deprecated in iOS 10.0 now need to use UserNotifications Framework's UNNotificationRequest.

Using UILocalNotification i was able to set fire date as well as repeat interval as below:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];
    localNotification.fireDate = tomorrow;


    if(repeatUnit!=0){
          localNotification.repeatInterval = NSWeekCalendarUnit;
    }

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

In above code I'm able to set tomorrows date when notification will get fire and also i was setting repeat interval of 1 week which is going to be started from fire date.

How this could be achieved using UserNotifications Framework's UNNotificationRequest.

Because in new api we can use either of trigger UNCalendarNotificationTrigger and UNTimeintervalNotificationTrigger to initiate UNNotificationRequest.

Is any way available so set both just like UILocalNotification does?


回答1:


You can still use the fire date and a repeat interval but it is not as obvious as it used to be. When you initialize the notification trigger you set its repeat parameter to YES. The repeat interval is defined by the date components that you pass to the trigger via the dateMatching parameter:

Repeat daily:
Just pass the date components hour, minute, second
⟶ The trigger will fire every day at that time

Repeat weekly:
Also pass the desired weekday component: weekday, hour, minute, second
⟶ The trigger will fire every week on that weekday at that time

Here is an example that fires a notification tomorrow at the same time and repeats it every week:

Objective-C:

UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];

// find out what weekday is tomorrow
NSDate *sameTimeTomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
NSInteger weekdayTomorrow = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate: sameTimeTomorrow];

// set the current time (without weekday)
unsigned unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute |  NSCalendarUnitSecond;
NSDateComponents *firstFireDateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:sameTimeTomorrow];

// add tomorrow's weekday
firstFireDateComponents.weekday = weekdayTomorrow;

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
UNCalendarNotificationTrigger *notificationTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:firstFireDateComponents repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"notification" content:notification trigger:notificationTrigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler: nil];

Swift:

let notification = UNMutableNotificationContent()

// find out what weekday is tomorrow
let weekDayTomorrow = Calendar.current.component(.weekday, from: Date(timeIntervalSinceNow: 60*60*24))

// set the current time (without weekday)
var firstFireDateComponents = Calendar.current.dateComponents([.hour, .minute, .second], from: Date())

// add tomorrow's weekday
firstFireDateComponents.weekday = weekDayTomorrow

// set a repeating trigger for the current time and tomorrow's weekday
// (trigger repeats every week on that weekday at the current time)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: firstFireDateComponents, repeats: true)

let request = UNNotificationRequest(identifier: "notification", content: notification, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


来源:https://stackoverflow.com/questions/47135461/how-to-set-repeat-interval-as-well-as-fire-date-in-unnotificationrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!