ios - 在 iOS 10 和 iOS9 中接收推送通知时遇到问题

标签 ios swift apple-push-notifications swift3

尝试为 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()

        }

这是在我的一个 View Controller 中登录 ^ 时调用的。

现在在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")
}

设备 token 作为数据上传到服务器,这在以前的版本中运行良好。
我已经检查过服务器上的 token 与手机上的 token 相匹配。
我在Capabilities中为所有目标启用了推送通知,我还勾选了Add the push Notifications entitlement to your entitlements file
没有收到任何东西尽管在推进中。
知道我在这里可能做错了什么吗?任何指针将不胜感激!

谢谢!

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

最佳答案

您是否检查过 Build Settings > Code Signing Entitlements 是否引用了 entitlements 文件? [AppName].entitlements.

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

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

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

关于ios - 在 iOS 10 和 iOS9 中接收推送通知时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39789521/

相关文章:

swift - 如何在快速闭包中实现单向引用

ios - 从远程服务器删除 iPhone 应用程序数据

iPhone 启动选项

ios - 在 View Controller 之间不断传递数据而无需使用segue?使用 Swift

ios - 白标企业 iOS 应用程序

ios - 如何使用主视图的选定单元格内容设置详细 View 的表格 View 的最顶层单元格(第一行)?

ios - 如何在 iOS 设备中获取唯一 ID?

swift - 如何比较场景套件中两个球体的大小?

ios - SKNode addChild 上的奇怪随机崩溃

iOS 未收到来自 Firebase 云消息传递的通知