• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 UNNotificationRequest 中设置重复间隔和触发日期

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:04:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

以前我在我的应用程序中使用 UILocalNotification 进行提醒。

但如上 API 在 iOS 10.0 中已弃用,现在需要使用 UserNotifications Framework 的 UNNotificationRequest

使用 UILocalNotification 我能够设置触发日期以及重复间隔,如下所示:

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

    NSDate *tomorrow = [NSDate dateWithTimeInterval24*60*60) sinceDate:[NSDate date]];
    localNotification.fireDate = tomorrow;


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

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

在上面的代码中,我可以设置明天的通知将触发的日期,并且我设置了 1 周的重复间隔,这将从触发日期开始。

如何使用 UserNotifications Framework 的 UNNotificationRequest 来实现。

因为在新的 api 中我们可以使用触发器 UNCalendarNotificationTriggerUNTimeintervalNotificationTrigger 来启动 UNNotificationRequest

有什么方法可以像 UILocalNotification 那样设置吗?



Best Answer-推荐答案


您仍然可以使用触发日期和重复间隔,但它不像以前那么明显了。初始化通知触发器时,将其重复参数设置为 YES。重复间隔由您通过 dateMatching 参数传递给触发器的日期组件定义:

每天重复:
只需传递日期组件 hourminutesecond
⟶ 触发器每天都会在那个时候触发

每周重复:
同时传递所需的工作日组件:weekdayhourminutesecond
⟶ 触发器将在每周的那个工作日触发

这是一个示例,它在明天的同一时间触发通知并每周重复一次:

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)

关于ios - 如何在 UNNotificationRequest 中设置重复间隔和触发日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135461/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap