2016-12-07 305 views
0

我最近在我的應用程序上設置了通知,我可以通過php發送,不用擔心,一切正常。通知iOs推送交付?

自上週以來,我試圖找出是否推送通知是由電話或沒有收到..

我可以知道,當有人點擊,但如果這個人不點擊它,喜歡打開應用程序,它不起作用..

是否沒有一個簡單的函數可以告訴我,如果通知剛剛收到應用程序?

在我AppDelegate.swift:

import UIKit 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

... 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    // Check if launched from notification 
    if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] { 
     //print(notification) 
     window?.rootViewController?.present(ViewController(), animated: true, completion: nil) 
    } 
    else{ 
     //print("ici ?") 
     registerForRemoteNotification() 
    } 

return true 
} 

... 

//Called when a notification is delivered to a foreground app. 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    completionHandler([.alert, .badge, .sound]) 
} 

//Called to let your app know which action was selected by the user for a given notification. 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    completionHandler() 
} 

... 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

     **print("here ?")** 

     switch((application.applicationState)){ 

     case UIApplicationState.inactive: 
      print("Inactive") 
      //Show the view with the content of the push 
      completionHandler(.newData) 

     case UIApplicationState.background: 
      print("Background") 
      //Refresh the local model 
      completionHandler(.newData) 

     default: 
      print("Active") 
      //Show an in-app banner 
      completionHandler(.newData) 
      break 
     } 
    } 

} 

問題:我從來沒有在didReceiveRemoteNotification通過:/ 「打印這裏」 永遠不會顯示。

我在這一行錯誤:

Instance method 'application(application:didReceiveRemoteNotification:fetchCompletionHandler:)' nearly matches optional requirement 'application(_:didReceiveRemoteNotification:fetchCompletionHandler:)' of protocol 'UIApplicationDelegate'

但我不明白:/

你有一個想法?

THX對您有所幫助^^

+0

無論你在你的第三解釋說,只有我們可以檢查用戶輕按通知。當用戶刷卡通知無法檢查時,此功能在蘋果文檔中不可用。 – Pavankumar

+0

無法檢查我的應用是否收到推送? O_o – Insou

+0

http://stackoverflow.com/questions/25830597/how-to-know-push-notification-delivery-status,去與鏈接,我研究也沒有保證說的交付與否。在我們的應用程序中,我們也嘗試了這一個,那時我才知道不可能,只有當用戶點擊那個時候纔可能。 – Pavankumar

回答

1

這是因爲轉換到斯威夫特3,而常見的錯誤 - 你用斯威夫特2方法。

在斯威夫特3這種方法的正確的簽名是:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 
    print("here?") 
} 
+0

哦,它只是一個糟糕的複製/粘貼......在我的代碼,這是正確的簽名,爲雨燕3 .. 我的壞^^ 但它仍然不工作:/ – Insou

+0

你在iOS 10中運行它嗎? – Elena

+0

是的,在iOs 10 .. – Insou