Swift3 重复通知Swift 3
我有只适用于一个时间项目的通知。我需要出示我的通知:晚上10:00和10:30。我怎么能做到?请告诉我 我的代码: NotificationManager.swift:Swift3 重复通知Swift 3,swift3,Swift3,我有只适用于一个时间项目的通知。我需要出示我的通知:晚上10:00和10:30。我怎么能做到?请告诉我 我的代码: NotificationManager.swift: import UIKit import UserNotifications class NotificationManager { static let shared = NotificationManager() let center = UNUserNotificationCenter.current() func re
import UIKit
import UserNotifications
class NotificationManager
{
static let shared = NotificationManager()
let center = UNUserNotificationCenter.current()
func registerNotifications()
{
center.requestAuthorization(options: [.sound,.alert], completionHandler: {( granted, error) in })
}
func addNotificationWithCalendarTrigger(hour: Int, minute: Int)
{
let content = UNMutableNotificationContent()
content.title = "Hi"
content.body = "It,s new notification!"
content.sound = UNNotificationSound.default()
var components = DateComponents()
components.hour = hour
components.minute = minute
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "calendar", content: content, trigger: trigger)
center.add(request) { (error) in
//handle error
}
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
timeForNotifications()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func timeForNotifications()
{
NotificationManager.shared.addNotificationWithCalendarTrigger(hour: 22, minute: 00)
}
}
可能有点晚了,但我想这就是你要找的。您可以使用以下代码将通知设置为在10:00显示:
var components = DateComponents()
components.hour = 22
components.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: "calendar", content: content, trigger: trigger)
我经常做的是用日期替换组件,但我认为您的选项也适用
var date = DateComponents()
date.hour = 22
date.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
希望这对你有帮助 您从未设置过委托中心。delegate=self也不要忘记为类设置UnuseNotificationCenterDelegate