当我第一次运行我的应用程序时,模拟器能够在应用程序处于前台时显示通知。
当我重新启动应用程序didReceive notification:
(即来自 iOS 9 的通知方法)而不是willPresent notification
(即来自 iOS 10 的通知方法)时,当应用程序处于前台时不会显示任何通知。但是,如果应用程序在后台,则会显示通知。
我使用以下代码获得通知的授权:
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]? = [:]) -> Bool {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .sound]) { (granted, error) in
UNUserNotificationCenter.current().delegate = self
}
}
return true
}
我已经实现了 willPresent 通知方法:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) completionHandler(UNNotificationPresentationOptions.alert)
}
以下是我创建通知的方式:
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = message
content.categoryIdentifier = identifier
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: false)
let request = UNNotificationRequest(
identifier: "identifier",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(
request, withCompletionHandler: nil)
简而言之问题:如何确保应用程序在第一次运行后应用程序处于前台时能够显示通知?
提前致谢。