ios - Swift:将本地通知转换为用户通知 IOS 10

标签 ios swift uilocalnotification nsusernotification

我最近将我的项目更新到 IOS 10,这导致我注册的所有本地通知都没有触发。我试图将本地通知转换为用户通知,但效果不同。这是我尝试转换的原始代码。在 viewDidLoad 中:

        guard let settings = UIApplication.shared.currentUserNotificationSettings else { return
        }
        if settings.types == UIUserNotificationType() {
            let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            present(ac, animated: true, completion: nil)
            return
        }


        var dateComp: DateComponents = DateComponents()
        dateComp.year = 2017;
        dateComp.month = 01;
        dateComp.day = 28;
        dateComp.hour = 11;
        dateComp.minute = 0;
        (dateComp as NSDateComponents).timeZone = TimeZone.current

        let calender:Calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        let date:Date = calender.date(from: dateComp)!


        let notification = UILocalNotification()
        notification.fireDate = date
        notification.alertBody = "Notification!"
        notification.alertAction = "You just Received a notification!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["customField1": "w00t"]
        notification.repeatInterval = NSCalendar.Unit.weekOfYear
        UIApplication.shared.scheduleLocalNotification(notification)

这导致本地通知在每个星期六上午 11:00 触发。

这是我用来尝试在 IOS 10 中实现相同效果的代码:

func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)

let content = UNMutableNotificationContent()
content.title = "Notification"
content.body = "You just Received a notification!"
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)


UNUserNotificationCenter.current().add(request) {(error) in
    if let error = error {
        print("Error")
    }
}
}

在viewDidLoad中:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
    if !accepted {
        print("Error")
    }
}

如果用户使用 datePicker 选择日期,此代码可以正常工作,但我无法弄清楚如何以编程方式将通知日期设置为我在本地通知中的相同日期 (2017-28-01 11 :00),以及如何在没有 repeatInterval 属性的情况下每周同时触发通知。

最佳答案

Could you explain how you could only specify for example Monday at 11:00 am

这只是忽略不适用于重复值的所有内容的问题。因此,在您的情况下,您想要指定日期组件 hour:11weekday:2 而不是其他任何内容。

要了解其工作原理,请在 Playground 上运行:

let dc = DateComponents(hour:11, weekday:2)
let greg = Calendar(identifier: .gregorian)
let d = greg.nextDate(after: Date(), matching: dc, matchingPolicy:.nextTime)

您会看到 d 是下周一上午 11 点。

这正是 UNNotificationTrigger 确定何时发送此通知所要做的。

现在,如果您还将此设置为 repeats 触发器,则您刚刚指定了每个星期一上午 11 点。

关于ios - Swift:将本地通知转换为用户通知 IOS 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911895/

相关文章:

ios - 从IOS通知中心一条条读取通知后如何清除?

iphone - UIsegmentedControl setTintColor 奇怪的行为

javascript - 如何为 iOS 和平板电脑设置 $.Scroll?

ios - Swift WKWebView 在 didFailProvisionalNavigation 上崩溃

ios - 使用 fetchrequest 谓词过滤掉某些核心数据项。

swift - 根据@State属性设置变量

ios - 快速将 NSString 转换为 NSDate

ios - iOS 的 UILocalNotification 中不显示操作和类别

ios - 向 UILocalNotification soundName 添加自定义声音

ios - 终止应用程序的本地横幅通知