繁体   English   中英

无法在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上传到服务器,这适用于以前的版本。
我检查过服务器上的令牌与手机上的令牌匹配。
我已经启用了推送通知在所有目标Capabilities ,我也选中Add the push Notifications entitlement to your entitlements file
在推动时根本没有收到任何东西。
我在这里做错了什么想法? 任何指针都会非常感激!

谢谢!

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

您是否检查过Build Settings> Code Signing Entitlements是否有对权利文件的引用? [AppName的] .entitlements。

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

(关键:APS环境,价值:发展)

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

刚刚注意到你在application(_:willFinishLaunchingWithOptions:)之后注册了委托application(_:willFinishLaunchingWithOptions:)或者调用了application(_:didFinishLaunchingWithOptions:)方法。

也许这就是为什么你的委托方法没有被击中?

查看文档

重要

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

要回答这个问题,也许对其他人有用。 我的令牌错了。 最近的更新编码已更改。 我实际上搜索了所有堆栈溢出,这是我的理解,我必须使用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设置委托对象应该是好的去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM