在 iOS10 和 iOS9 中接收推送通知时遇到问题

Trouble receiving push notifications in iOS10 and iOS9

正在尝试为 iOS10 设置推送通知。它以前是否适用于 10 以下的版本。
通读一些指南,我的设置是这样的:

// Register for the defined notifications

        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.delegate = UIApplication.shared.delegate as! AppDelegate
            center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

                if error == nil {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }

        } else {

            // Fallback on earlier versions

            let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory])
            UIApplication.shared.registerUserNotificationSettings(notificationSettings)
            UIApplication.shared.registerForRemoteNotifications()

        }

这是在我的一个视图控制器中登录 ^ 时调用的。

而现在AppDelegate,它符合UNUserNotificationCenterDelegate,我有这些方法:

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


@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Got a push!")
}

我还有:

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let settings = UserDefaults.standard
    settings.setValue(deviceToken, forKey: "deviceToken")
}

设备令牌作为 Data 上传到服务器,这在以前的版本中运行良好。
我检查过服务器上的令牌与 phone.
我已经为 Capabilities 中的所有目标启用了推送通知,我还勾选了 Add the push Notifications entitlement to your entitlements file
虽然在推送时根本没有收到任何东西。
知道我在这里做错了什么吗?任何指针将不胜感激!

谢谢!

编辑:我还注意到推送通知在 iOS9 中不起作用。

您是否检查过“构建设置”>“代码签名权利”是否引用了权利文件? [AppName].entitlements.

权利文件包含正确的信息:

(键:APS 环境,值:开发)

除此之外,您可以尝试重新生成推送证书。我们必须创建新证书才能在 IOS 10 升级后(但不是在沙箱环境中)在生产环境中运行。

刚刚注意到您注册委托的时间晚于 application(_:willFinishLaunchingWithOptions:)application(_:didFinishLaunchingWithOptions:) 方法被调用的时间。

也许这就是您的委托方法未被命中的原因?

参见 docs:

Important

You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching. For example, in an iOS app, you must assign it in the application(:willFinishLaunchingWithOptions:) or application(:didFinishLaunchingWithOptions:) method.

准备回答这个问题,也许对其他人有用。我的令牌错了。编码随着最近的更新而改变。我确实为此搜索了整个堆栈溢出,据我了解,我必须使用 base64stringData 转换为 String。但是我注意到这个字符串中有一些奇怪的字符,例如 \。我最终做了一个 Data 扩展:

extension Data {

    func hexString() -> String {
        return self.reduce("") { string, byte in
            string + String(format: "%02X", byte)
        }
    }

}

这会将 Data 转换为正确的格式。因此,如果这是您进行转换的方式,并且设置了所有权利,您记得添加新方法:

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


@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Got a push!")
}

并在 didFinishLaunchingWithOptions 中设置委托对象,一切都应该顺利进行。