当我的iOS应用程序从远程通知打开时(当用户点击推送通知以从终止状态打开应用程序时),它会启动错误(没有启动图像、未观察到安全区域、选项卡栏不显示等).我不希望推送通知有任何特殊功能,我希望它尽可能简单.
以下是我在App Delegate中配置通知的方式:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupUserNotifications()
return true
}
private func setupUserNotifications() {
let notifications = UNUserNotificationCenter.current()
let connectionMessages = UNNotificationCategory(identifier: "connectionMessageNotification",
actions: [],
intentIdentifiers: [],
options: UNNotificationCategoryOptions.init())
let interactionMessages = UNNotificationCategory(identifier: "interactionMessageNotification",
actions: [],
intentIdentifiers: [],
options: UNNotificationCategoryOptions.init())
notifications.delegate = self
notifications.setNotificationCategories([connectionMessages, interactionMessages])
notifications.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
} else {
print(if: error)
}
}
}
这就是我处理委托的方式(什么都不做,只是调用completion):
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("did tap remote notification")
completionHandler()
}
有没有我错过的一步?我应该执行application(_:willFinishLaunchingWithOptions:)
吗?我不明白的是,如果用户通过点击推送通知来启动应用程序,应用程序难道不应该正常启动而不需要采取任何额外的步骤吗?