【问题标题】:Trouble receiving push notifications in iOS10 and iOS9在 iOS 10 和 iOS 9 中无法接收推送通知
【发布时间】:2016-09-30 10:36:04
【问题描述】:

尝试为 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 的形式上传到服务器,这在以前的版本中运行良好。
我已检查服务器上的令牌与手机上的令牌是否匹配。
我已启用推送Capabilities 中所有目标的通知,我还勾选了Add the push Notifications entitlement to your entitlements file
在推送时根本没有收到任何东西。
有什么想法我可能在这里做错了吗?任何指针将不胜感激!

谢谢!

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

【问题讨论】:

标签: ios swift apple-push-notifications swift3


【解决方案1】:

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

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

(key:APS Environment,value:开发)

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

【讨论】:

  • 检查了构建设置 > 代码签名权利,那里似乎没有问题。
  • @Kex 在我们公司的应用程序数量方面遇到了同样的问题。还请检查构建设置 > 功能并确保“通知”已打开。
【解决方案2】:

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

也许这就是你的委托方法没有被命中的原因?

docs

重要

您必须在应用完成启动之前将您的委托对象分配给 UNUserNotificationCenter 对象。例如,在 iOS 应用中,您必须在 application(:willFinishLaunchingWithOptions:) 或 application(:didFinishLaunchingWithOptions:) 方法中分配它。

【讨论】:

  • 真是个好地方!我回家后会试一试,然后告诉你结果。
  • 已将 let center = UNUserNotificationCenter.current() ; center.delegate = self 移至 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool,但仍然没有任何结果
  • 我的地址是:if #available(iOS 10.0, *) { UNUserNotificationCenter.currentNotificationCenter().delegate = self }
  • 我猜你还没有使用 Swift 3?
  • 你是对的。尽管在正确的位置定义代理至少是一个开始,但还有其他一些东西阻止您收到这些通知。
【解决方案3】:

要回答这个问题,也许它对其他人有用。我的令牌错了。编码随着最近的更新而改变。我确实为此搜索了整个堆栈溢出,据我了解,我必须使用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 中设置委托对象,一切都会好起来的。

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 2018-04-22
    • 2017-06-13
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多