0
通知は1回限りの項目でうまくいきます。午後10時と午後10時30分に通知する必要があります。どのように私はそれを行うことができますか?繰り返し通知3回
NotificationManager.swift:
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)
}
}
デリゲートを決して設定しない center.delegate = self また、クラスのUNUserNotificationCenterDelegateを設定することを忘れないでください – t1ser