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

Posted

技术标签:

【中文标题】IOS启动时如何区分通知是本地通知还是远程通知【英文标题】:How to differentiate between whether a notification is a local or remote notification when app Starts in IOS 【发布时间】:2019-02-07 07:05:52 【问题描述】:

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

    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) 

【问题讨论】:

【参考方案1】:

您也可以在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");
    


【讨论】:

这仅在应用程序处于前台或后台状态时有效。当应用程序因本地通知情况而被杀死时,它不起作用【参考方案2】:

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

以下是使用标识符创建本地通知的示例。

        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)

处理带有标识符的本地通知。

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()


对于处理远程通知,您可以使用以下行

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

您可以通过检查第一个中的标识符来添加一个附加条件,以相同的方法处理这两个通知 行。

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

【讨论】:

【参考方案3】:

您可以在 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 键用户信息数据,现在您可以识别天气通知是远程本地的。

【讨论】:

以上是关于IOS启动时如何区分通知是本地通知还是远程通知的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发--本地通知与远程通知

iOS如何在本地通知上启动后台任务

iOS 在收到静默远程通知后发送本地通知

当 iOS 10 应用程序在后台时,有啥方法可以触发本地通知?

如何检测用户是不是采取了本地通知或远程通知的操作?

IOS,应用启动时如何处理多个本地通知?