之前我使用下面的代码来区分当应用程序启动时我的通知是本地的还是远程的
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{
}
if (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil)
{
}
}
条件是我的应用程序被终止,我正在从通知中打开它。
问题是这个方法
if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{
}
已弃用,从通知中心打开应用时不会调用以下方法
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
最佳答案
你也可以在 userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
中检查通知类型,
类层次结构是:
UNNotificationResponse
>UNNotification
>UNNotificationRequest
>UNNotificationTrigger
UNNotificationRequest
中有4种类型的触发器:
UNLocationNotificationTrigger
UNPushNotificationTrigger
UNTimeIntervalNotificationTrigger
UNCalendarNotificationTrigger
随便用,
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.trigger is UNPushNotificationTrigger {
print("remote notification");
}
}
关于ios - iOS应用启动时如何区分通知是本地通知还是远程通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567970/