2016-09-24 20 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

相关问题