How to set repeat frequency in User Notification [duplicate]

你离开我真会死。 提交于 2019-11-28 21:39:43

For iOS 10 you can use like this:

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

it has the same effect from the iOS 9 code. To repeat you just have to use the components that you want to repeat.

Libin Lu

inspired by @Ramon Vasconcelos, I'm using the following code for setting up intervals and fireDate together

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