簡體   English   中英

iOS swift 應用程序被殺死時如何打開推送通知“鏈接”

[英]iOS How to open Push Notification "link" when app is killed in swift

我想在應用程序被終止時單擊推送通知並打開“鏈接” 當應用程序被終止時,我應該在哪里控制? 我怎樣才能打開“鏈接”?

也適用於前景和背景。 我正在使用這段代碼。

如果您能提供幫助,我將不勝感激,謝謝!

override func viewDidLoad() {
    super.viewDidLoad()
    print("viewDidLoad")

    // background
    NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { (Notification) in
        
        let userDefault = UserDefaults.standard
        let pushUrl:String? = userDefault.string(forKey: "PUSH_URL")
        

        if(pushUrl != nil){
            NSLog(pushUrl!)
            
            let myUrl = URL(string: pushUrl!)
            let myRequest = URLRequest(url: myUrl!)
            self.webView.load(myRequest)
            
            userDefault.removeObject(forKey: "PUSH_URL")
            userDefault.synchronize()
            
            
        }
     }
     // foreground
     NotificationCenter.default.addObserver(self, selector: #selector(didRecieveTestNotification(_:)), name: NSNotification.Name("TestNotification"), object: nil)

}

@objc func didRecieveTestNotification(_ notification: Notification) {
    print("Test Notification")
    let userDefault = UserDefaults.standard
    let pushUrl:String? = userDefault.string(forKey: "PUSH_URL")
    
    if(pushUrl != nil){
        NSLog(pushUrl!)
        
        let myUrl = URL(string: pushUrl!)
        let myRequest = URLRequest(url: myUrl!)
        self.webView.load(myRequest)
        
        userDefault.removeObject(forKey: "PUSH_URL")
        userDefault.synchronize()
        
        
    }

單擊通知后,應用程序被殺死時無法控制推送通知

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

該方法將被調用,您可以根據您的通知類型打開您的鏈接。

您也可以使用此方法並獲取您的通知。

    func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler _: @escaping (UNNotificationPresentationOptions) -> Void) {}

讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM