我有一个Webview ios应用程序,该应用程序接收通知,并传递一个url,以便用户单击通知时,它将打开该URL的webview。
当应用程序在前台和后台运行时,它可以正常工作。如果用户在应用程序关闭且当前未运行时收到通知,则该应用程序将打开,但不会转到该网址
在我的didReceiveRemoteNotification
中,我检测到应用程序的不同状态,但我认为.background的工作原理与未运行相同,但我猜不行。关闭应用程序后,如何获取打开URL的通知?
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
//print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
else if state == .inactive {
// inactive
//print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
更新
因此,在一些帮助下,我已经能够使用didFinishLaunchingWithOptions
来调用我的Web视图,但是在按下时的通知仍未打开该URL。
我在我的代表的其他一些区域使用viewController?.loadRequestnotification(for: url as! String)
正常工作。我怀疑return true
可能与通话冲突。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] as? [String : AnyObject] {
let url = object["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
return true
}
答案 0 :(得分:1)
didReceiveRemoteNotification。
在您的应用关闭时尝试使用此代码以获取通知数据。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
答案 1 :(得分:0)
有一种情况,例如您的应用未运行,并且用户单击应用的通知,然后按照获取方式进行操作。
这是您可以获取的代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: \(dictionary)")
}
}
}
您的应用程序可以收到一些通知,它位于通知中心,但是用户无法单击任何通知,但是他们会像平常一样打开您的应用程序,然后通过以下方法,您可以获取您的接收到的所有通知应用。
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
答案 2 :(得分:0)
这是当应用收到任何通知时调用的函数。 我已经在聊天应用程序中使用了它。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
如果应用已打开,并且您希望在收到通知时执行某些操作 使用这种方法
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
您还可以通过通知查看应用是否打开的天气
AppDelegate's didFinishLaunchingWithOptions
但是建议将此didFinishLaunchingWithOptions
方法保持尽可能浅。
我希望这对您有用