WKWebView在本地通知点击上打开URL

时间:2018-03-07 11:41:54

标签: ios swift notifications

我有PHP Web服务器通过firebase将数据消息发送到我的iOS WKWebView应用程序,我需要在WKWebView中打开收到数据消息的链接。

我已经设置了正确的开发者帐户,firebase和iOS应用程序,所以我收到了像这样的远程通知

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    let body = userInfo["body"] as! String;
    let title = userInfo["title"] as! String;

    if (UIApplication.shared.applicationState == .inactive || UIApplication.shared.applicationState == .background)
    {
        localUserNotification(title: title, body: body);
    }

    completionHandler(UIBackgroundFetchResult.newData)
}

因为我的推送通知实际上是一个数据消息,我需要构建,我正在构建我的本地通知。

func localUserNotification(title: String, body: String){

    let center = UNUserNotificationCenter.current()

    let options: UNAuthorizationOptions = [.alert, .sound];
    center.requestAuthorization(options: options) {
        (granted, error) in
        if !granted {
            print("Something went wrong")
        }
    }

    let content = UNMutableNotificationContent()
    content.title = title
    content.body = body
    content.userInfo = ["link": "https://google.com"];
    content.sound = UNNotificationSound.default()

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


    let identifier = "UYLLocalNotification"
    let request = UNNotificationRequest(identifier: identifier,
                                        content: content, trigger: trigger)

    center.add(request);
}

因为link不是UNMutableNotificationContent的有效参数我在userInfo对象中传递它,此时我有工作代码,当我从以下位置接收数据消息时,会向我显示本地通知我的服务器通过firebase,现在我需要处理这个通知的用户点击我想用这个方法做什么

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

    if (UIApplication.shared.applicationState == .inactive || UIApplication.shared.applicationState == .background)
    {
        print(response);
    }


    completionHandler();
}

但是响应没有关于本地通知中发送的userInfo的数据,我看到的只有

<UNNotificationResponse: 0x1c42250e0; actionIdentifier: com.apple.UNNotificationDefaultActionIdentifier, notification:     
<UNNotification: 0x1c42259a0; date: 2018-03-07 11:30:47 +0000, request:     
<UNNotificationRequest: 0x1c403b220; identifier: UYLLocalNotification, content: <UNNotificationContent: 0x1c4112a20; title: This is magic, subtitle: (null), body: Well hello there mister, categoryIdentifier: , launchImageName: , peopleIdentifiers: (
), threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x1c40bbd80>, hasDefaultAction: YES, defaultActionTitle: (null), shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x1c42218c0; repeats: NO, timeInterval: 0.100000>>>>

我如何访问我的链接?

1 个答案:

答案 0 :(得分:0)

尝试

print(response.notification.request.content.userInfo)