热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何每天重复UNUserNotification?

如何解决《如何每天重复UNUserNotification?》经验,为你挑选了1个好方法。

我正在尝试允许用户安排通知,以便在每天的特定时间打开应用.到目前为止,我已经能够通过计算从现在到用户选择的时间以及在X秒内安排通知的时间来安排第一个通知.但是,有没有办法让我可以设置每天重复的通知?如果您感到困惑,这是我的代码的一部分:

let newTime: Double = Double(totalDifference)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)
let request = UNNotificationRequest(identifier: "openApp", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
    if error != nil {
        print(error!)
        completion(false)
    } else {
        completion(true)
    }
})

任何帮助深表感谢



1> Teja Nandamu..:

在您的情况下,更改此行:

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: false)

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: newTime, repeats: true)

从文档:

UNCalendarNotificationTrigger:

在指定的日期和时间触发通知.您使用UNCalendarNotificationTrigger对象指定通知的触发条件的时间信息.日历触发器可以触发一次,也可以多次触发.

NSDateComponents* date = [[NSDateComponents alloc] init];
date.hour = 8;
date.minute = 30; 
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                     triggerWithDateMatchingComponents:date repeats:YES];

迅速:

var date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)


推荐阅读
author-avatar
手浪用户2602890763
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有