如何在用户通知中设置重复频率

S.J*_*S.J 9 objective-c nsdate nsdatecomponents ios uilocalnotification

直到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];
Run Code Online (Sandbox Code Playgroud)

我们在本地通知中repeatInterval,现在在WWDC2016Apple宣布User Notification其中包含

  • UNTimeIntervalNotificationTrigger.
  • UNCalendarNotificationTrigger.

    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
    
    Run Code Online (Sandbox Code Playgroud)

上面的代码会在每分钟后触发通知.但无法设定日期.

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

UNCalendarNotificationTrigger* triggerC = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:date repeats:YES];
Run Code Online (Sandbox Code Playgroud)

上面的代码可以设置,重复将在明天8:30触发,而不是在分钟后.

在iOS 10中User Notification我如何设置重复频率的日期时间就像我们可以设置的一样UILocalNotification

我希望User Notification明天晚上8:30 安排,并在每分钟后继续重复,就像我在顶部指定的代码一样local notification

Ram*_*los 0

对于 iOS 10,你可以这样使用:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate];
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
Run Code Online (Sandbox Code Playgroud)

它与 iOS 9 代码具有相同的效果。要重复,您只需使用要重复的组件即可。


归档时间:

查看次数:

3861 次

最近记录:

8 年,11 月 前