2017-02-09 32 views
1

在我的應用程序中,當我的通知動作被點擊時,我想要一個URL運行。問題是,當我運行應用程序它把我放在前臺,但不運行URL(我添加.foreground通知行動)。這裏是我的代碼:我點擊我的通知操作後,我的網址沒有運行。我該怎麼辦?

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

    if response.actionIdentifier == "call" { 


      if let urled = URL(string: "tel://1234567891") { 
       UIApplication.shared.open(urled, options: [:])        
      }     

    } 


} 
} 

任何人的幫助將是偉大的:)感謝您的任何答案!

+0

哪裏變量'number'被置或訪問?我沒有看到任何地方提及它。 – creeperspeak

+0

@creeperspeak變量'數字'設置在函數的正上方,我只是不想分享任何電話號碼 – krish

+0

好吧,你確認'if let urled'正在通過嗎?你可以嘗試調用'canOpenUrl'來查看url是否是一個有效的格式。你有沒有試過打印'「tel:// \(number)」'看它看起來是否正確?如果'number'是可選的,則需要先解包。可能是很多事情,但是打印語句和斷點可以爲您解決問題。 – creeperspeak

回答

0

對於iOS 10: 將UserNotifications.framework添加到您的應用。 請檢查您是否設置了推送通知的委託。 如果你已經設置它,然後嘗試iOS的10

以下方法
import UserNotifications 

@UIApplicationMain 

class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate { 
} 



func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 
    print("Handle push from foreground") 
    // custom code to handle push while app is in the foreground 
    print("\(notification.request.content.userInfo)") 

} 


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

print("Handle push from background or closed") 

    // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 

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

} 
+0

請檢查此鏈接的Objective C:http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10?answertab=votes#tab-top –

0

試試這個:

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

if response.actionIdentifier == "call" { 

    if let urled = URL(string: "tel://\(1234567891)") { 
     if UIApplication.shared.canOpenURL(urled) { 
      UIApplication.shared.open(urled, options: [:], completionHandler: { (completed) in 
       // completionHandler block 
      }) 
     } 
    } 
} 
completionHandler()} 
0

我想同樣的事情,我面臨着同樣的問題。

如果您正在使用通知內容擴展,您可以嘗試替代解決方案。

由於我們可以處理兩個Extension通知行動以及Containing App,則可以使用的extensionContext代替Containing App's UIApplication object嘗試處理它在Extension

試試這個:

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) 
    { 
     if response.actionIdentifier == "call" 
     { 
      self.extensionContext?.open(URL(string: "tel:9555221836")!, completionHandler: { (done) in 
       completion(.dismiss) 
      }) 
     } 
     //Handling other actions... 
    } 
相關問題