如何在应用程序启动时区分通知是本地通知还是远程通知 IOS

How to differentiate between whether a notification is a local or remote notification when app Starts in IOS

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

    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:中查看通知类型,

Class 层次结构为:

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

}

在创建本地通知时设置通知中的标识符,可用于识别处理通知中的差异

Following is example creating localnotification with identifier.

        let content = UNMutableNotificationContent()
        content.title = "Title"
        content.body = "Body"
        content.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

        let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

Handling local notification with identifier.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    if response.notification.request.identifier == "TestIdentifier" {
        print("handling notifications with the TestIdentifier Identifier")
    }

    completionHandler()

}

For Handling remote notification you can use the following line

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("handling notification")
    if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
        let message = parseRemoteNotification(notification: notification)
        print(message as Any)
    }
    completionHandler()
}
 
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
    if let aps = notification["aps"] as? [String:AnyObject] {
        let alert = aps["alert"] as? String
        return alert
    }
 
    return nil
}

You can add an additional condition for handling both the notification in the same method by checking identifier in the first line.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("handling notification")
 if response.notification.request.identifier == "TestIdentifier" {
        print("handling notifications with the TestIdentifier Identifier")
    }else {
    if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
        let message = parseRemoteNotification(notification: notification)
        print(message as Any)
    }
}
    completionHandler()
}
 
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
    if let aps = notification["aps"] as? [String:AnyObject] {
        let alert = aps["alert"] as? String
        return alert
    }
 
    return nil
}

您可以在 content.userInfo 中设置本地通知键值。

content.userInfo = ["isMyLocationNotification" : true] //you can set anything

然后查看UNUserNotificationCenterDelegate的didReceive响应方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            print(response.notification.request.content.userInfo) //you can check your notification types
        }

在输出部分,您将使用 isMyLocationNotification 键的用户信息数据,现在您可以识别天气通知是本地的还是远程的。