2016-09-24 25 views
1

昨天我更新我的應用程序的2至3迅速iOS上的10推輓沒有收到當應用程序在前臺

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { 
    UIApplication.shared.registerForRemoteNotifications() 
} 

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print(error.localizedDescription) 
} 



func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    // Save Installation if registered successfully 
} 


//MARK: Recieved Notification 
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
    print("received a notification") 

} 

這樣的背景推送通知工作正常,在兩個應用程序,但在我的應用程序之一,其不接收前景是任何推,didReceiveRemoteNotification是從來沒有得到所謂的

事情我已經檢查,推動在能力

使用此代碼啓用的通知,爲推動notific註冊在這兩個應用

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)) 

不僅如此ations,我已經試過UNUserNotificationCenterDelegate爲iOS 10,但仍然沒有它的代表功能調用。

這隻適用於iOS 10手機,iOS 8和9就像魅力一樣。 所以,我真的不知道爲什麼它只有我2 SWIFT 3 IOS 10應用程序之一,從來沒有打電話didReceiveRemoteNotification當應用程序打開時

這是我嘗試

//Added in didFinishLaunchingWithOptions 
    if #available(iOS 10.0, *) { 
       let center = UNUserNotificationCenter.current() 
       center.delegate = self 
       center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in 
        if error == nil{ 
         UIApplication.shared.registerForRemoteNotifications() 
        } 
       } 
      } 

//Delegates 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     print("push2") 
    } 

    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     print("push1") 
    } 
+0

'UIApplication.shared.registerUserNotificationSettings'但是,這不是你怎麼做iOS中10.你使用Ť他請UNUserNotificationCenter註冊。你的'application:didReceiveRemoteNotification'等等完全不相關。你需要重寫_completely_。觀看相關的WWDC 2016視頻(其中有兩個)。 – matt

+0

@matt,因爲我在問題中寫道,我已經嘗試過UNUserNotificationCenter的代理和新的註冊代碼,但它仍然沒有工作,我試過這個http://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called -ios-10以及舊的代碼也適用於我的其他應用程序多數民衆贊成我的問題說 – user528432

+0

如果您正在編譯對iOS 10您必須切換到UNUserNotificationCenter。因此,您有兩種選擇:切換或不針對iOS 10進行編譯。您沒有顯示任何UNUserNotificationCenter代碼,因此我沒有理由相信您做得對。 – matt

回答

1
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     self.initializeNotificationServices() 
} 

func initializeNotificationServices() -> Void { 

     if #available(iOS 10.0, *) { 

      UNUserNotificationCenter.currentNotificationCenter().delegate = self 
      UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in 
       if granted { 
        //self.registerCategory() 
        //self.scheduleNotification("test", interval: 3) 
        //self.scheduleNotification("test2", interval: 5) 
        let types : UIUserNotificationType = [.Badge, .Sound, .Alert] 
        let mySettings : UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: types, categories: nil) 
        UIApplication.sharedApplication().registerUserNotificationSettings(mySettings) 
        UIApplication.sharedApplication().registerForRemoteNotifications() 

       } 
      } 
     } 
     else { 

      // Fallback on earlier versions 
      let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil) 
      UIApplication.sharedApplication().registerUserNotificationSettings(settings) 

      // This is an asynchronous method to retrieve a Device Token 
      // Callbacks are in AppDelegate.swift 
      // Success = didRegisterForRemoteNotificationsWithDeviceToken 
      // Fail = didFailToRegisterForRemoteNotificationsWithError 
      UIApplication.sharedApplication().registerForRemoteNotifications() 
     } 
    } 

    @available(iOS 10.0, *) 
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
     print("willPresent") 
     completionHandler([.Badge, .Alert, .Sound]) 
    } 

    @available(iOS 10.0, *) 
    func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
     self.didReceiveBookingAppRemoteNotification(response.notification.request.content.userInfo) 
     print("didReceive == >> \(response.notification.request.content.userInfo)") 
     completionHandler() 
    } 


//for Lower to ios 10 version 
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     self.getNotifInfo(userInfo) 
    } 
我的iOS代碼10

確保您必須啓用從項目的 目標=>功能推送通知,你必須增加它的框架UserNotifications.framework

相關問題