5秒後にアプリからの通知が来るようにしたい
環境
コード
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests();
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in if granted { print("通知許可")}
}
let content = UNMutableNotificationContent()
content.title = "通知タイトル"
content.body = "通知本文"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "HogehogeNotification", content: content, trigger: trigger)
center.add(request)
center.delegate = self
}
}
確認