本地用户通知不是在后台获取时创建的
Local user notifications not created on background fetch
每次应用程序执行后台获取或与蓝牙设备同步时,我都需要创建一个本地 UserNotification。问题是 iOS 10 之前的旧本地通知有效,而现在新的 UserNotifications work/trigger 无效,没有任何反应。这个问题有什么解决办法吗?
目前我的测试代码如下:
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
print("NOT authorized")
}
}
let content = UNMutableNotificationContent()
content.title = "MyApp"
content.body = "oi"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let notification = UNNotificationRequest(identifier: "timer", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(notification) { (error) in
if error != nil {
print("DID NOT CREATE NOTIFICATION")
}else{
print("CREATED NOTIFICATION")
}
}
扩展 AppDelegate:UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: > @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, > >didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
} }
好的,我找到问题了,我缺少请求授权码。
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
每次应用程序执行后台获取或与蓝牙设备同步时,我都需要创建一个本地 UserNotification。问题是 iOS 10 之前的旧本地通知有效,而现在新的 UserNotifications work/trigger 无效,没有任何反应。这个问题有什么解决办法吗?
目前我的测试代码如下:
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
print("NOT authorized")
}
}
let content = UNMutableNotificationContent()
content.title = "MyApp"
content.body = "oi"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
let notification = UNNotificationRequest(identifier: "timer", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(notification) { (error) in
if error != nil {
print("DID NOT CREATE NOTIFICATION")
}else{
print("CREATED NOTIFICATION")
}
}
扩展 AppDelegate:UNUserNotificationCenterDelegate {
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: > @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, > >didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
} }
好的,我找到问题了,我缺少请求授权码。
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
else {
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}