IOS中app启动时如何区分通知是本地通知还是远程通知

Ahm*_*med 4 ios localnotification swift

以前我使用以下代码来区分应用程序启动时我的通知是本地还是远程

    func application(_ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: 
    [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
    {


    }
    if (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil)
    {


    }
    }
Run Code Online (Sandbox Code Playgroud)

条件是我的应用程序被杀死,我正在从通知中打开它。

问题是这个方法

if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{


}
Run Code Online (Sandbox Code Playgroud)

已弃用,从通知中心打开应用程序时不会调用以下方法

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
Run Code Online (Sandbox Code Playgroud)

Ank*_*wal 10

您也可以检查通知类型userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:

类层次结构是:

UNNotificationResponse> UNNotification> UNNotificationRequest>UNNotificationTrigger

有 4 种类型的触发器UNNotificationRequest

  • 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");
    }

}
Run Code Online (Sandbox Code Playgroud)