gpt4 book ai didi

ios - WKWebView 在本地通知上打开 URL

转载 作者:行者123 更新时间:2023-11-30 11:42:51 25 4
gpt4 key购买 nike

我有 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>>>>

我如何访问我的链接?

最佳答案

尝试

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

关于ios - WKWebView 在本地通知上打开 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com