swift 通知未触发
swift notification not triggering
我是 swift 中通知的新手,在我的 AppDelegate.swift 文件中设置了以下内容。它运行并且我得到通知已设置并且没有错误的打印输出。我还获得了请求通知的权限,我单击“是”。我相信我的间隔通知应该在启动后 20 秒内触发。它不会触发,如果我在应用程序中或已最小化应用程序,这不会改变。请注意,我在 XCODE 的模拟器模式下 运行。为什么我的通知没有触发?
func setupNotifications(){
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yay!")
} else {
print("D'oh")
}
}
notificationCenter.removeAllPendingNotificationRequests()
print("setting up notifications")
let content = UNMutableNotificationContent()
content.title = "Answer your daily questions"
content.body = "Answer your daily questions please"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
//let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (1*60), repeats: true)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
// Create the request
let request = UNNotificationRequest(identifier: "daily-notifier",
content: content, trigger: trigger)
// Schedule the request with the system.
notificationCenter.add(request) { (error) in
if error != nil {
print("there were errors to address")
// Handle any errors.
}
}
print("done setting up notifications")
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
print("\(path)")
// Override point for customization after application launch.
setupNotifications()
return true
}
首先请注意,根据 Apple 的 requestAuthorization(options:completionHandler:)
文档,您可能应该先等待获得许可,然后再添加本地通知请求
Note: Always call this method before scheduling any local notifications and before registering with the Apple Push Notification service.
还要确保您已将推送通知添加为应用程序的一项功能,因为如果您提交它,将需要它。
在我 运行 上面的代码之后,当应用程序仍然处于活动状态时我没有收到通知,但是当应用程序处于后台时我确实收到了通知。我相信,如果用户收到处于活动状态的应用程序的通知,它会以静默方式发出;即,没有横幅和声音(至少这是我对通知的体验,尽管可能还有更多)。
我是 swift 中通知的新手,在我的 AppDelegate.swift 文件中设置了以下内容。它运行并且我得到通知已设置并且没有错误的打印输出。我还获得了请求通知的权限,我单击“是”。我相信我的间隔通知应该在启动后 20 秒内触发。它不会触发,如果我在应用程序中或已最小化应用程序,这不会改变。请注意,我在 XCODE 的模拟器模式下 运行。为什么我的通知没有触发?
func setupNotifications(){
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
print("Yay!")
} else {
print("D'oh")
}
}
notificationCenter.removeAllPendingNotificationRequests()
print("setting up notifications")
let content = UNMutableNotificationContent()
content.title = "Answer your daily questions"
content.body = "Answer your daily questions please"
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
//let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (1*60), repeats: true)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 20, repeats: false)
// Create the request
let request = UNNotificationRequest(identifier: "daily-notifier",
content: content, trigger: trigger)
// Schedule the request with the system.
notificationCenter.add(request) { (error) in
if error != nil {
print("there were errors to address")
// Handle any errors.
}
}
print("done setting up notifications")
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let path = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
print("\(path)")
// Override point for customization after application launch.
setupNotifications()
return true
}
首先请注意,根据 Apple 的 requestAuthorization(options:completionHandler:)
Note: Always call this method before scheduling any local notifications and before registering with the Apple Push Notification service.
还要确保您已将推送通知添加为应用程序的一项功能,因为如果您提交它,将需要它。
在我 运行 上面的代码之后,当应用程序仍然处于活动状态时我没有收到通知,但是当应用程序处于后台时我确实收到了通知。我相信,如果用户收到处于活动状态的应用程序的通知,它会以静默方式发出;即,没有横幅和声音(至少这是我对通知的体验,尽管可能还有更多)。