当应用处于前台时,在iOS 10上未收到推送

时间:2016-09-24 15:19:56

标签: swift swift3 ios10

昨天我将我的2个应用更新为swift 3。

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 永远不会被调用

我检查过的内容,在功能

中启用推送通知

使用此代码注册两个应用上的推送通知

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

不仅如此,我还尝试过针对iOS 10的 UNUserNotificationCenterDelegate ,但仍未调用其委托函数。

这只适用于iOS 10手机,iOS 8和9的功能就像魅力一样。 所以我不确定为什么它在应用程序打开时只在ios 10上的2个swift 3应用程序中的一个中调用didReceiveRemoteNotification

这是我试过的iOS 10代码

//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")
    }

1 个答案:

答案 0 :(得分: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)
    } 

确保您必须启用项目的推送通知 目标=>功能,您必须添加它的框架UserNotifications.framework