简体   繁体   English

无法在iOS10和iOS9中接收推送通知

[英]Trouble receiving push notifications in iOS10 and iOS9

Trying to set up push notification for iOS10. 尝试为iOS10设置推送通知。 had it working previously for versions below 10. 以前它是否适用于低于10的版本。
Read through some guides my setup is like so: 阅读一些指南我的设置是这样的:

// 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()

        }

This is called on login ^ in one of my view controllers. 这是在我的一个视图控制器中登录^时调用的。

And now in AppDelegate , it conforms to UNUserNotificationCenterDelegate , I have these methods: 现在在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!")
}

And I also have: 我也有:

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

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

The device token is uploaded to the server as Data , and this has worked fine for previous versions. 设备令牌作为Data上传到服务器,这适用于以前的版本。
I have checked that the token on the server matches the one on the phone. 我检查过服务器上的令牌与手机上的令牌匹配。
I have enabled push notifications for all targets in Capabilities , I have also ticked Add the push Notifications entitlement to your entitlements file . 我已经启用了推送通知在所有目标Capabilities ,我也选中Add the push Notifications entitlement to your entitlements file
Not receiving anything at all though on the push. 在推动时根本没有收到任何东西。
Any ideas what I might be doing wrong here? 我在这里做错了什么想法? Any pointers would be really appreciated! 任何指针都会非常感激!

Thanks! 谢谢!

EDIT: I have also noticed that the push notifications are not working in iOS9. 编辑:我也注意到推送通知在iOS9中不起作用。

Have you checked that Build Settings > Code Signing Entitlements has a reference to entitlements file? 您是否检查过Build Settings> Code Signing Entitlements是否有对权利文件的引用? [AppName].entitlements. [AppName的] .entitlements。

And that the entitlements file contains the correct info: 并且权利文件包含正确的信息:

(key: APS Environment, value: development) (关键:APS环境,价值:发展)

Other than that, you can try regenerating push certificates. 除此之外,您可以尝试重新生成推送证书。 We had to create new certificates to get it to work in production after IOS 10 upgrade (but not in sandbox environment). 我们必须创建新证书,以便在IOS 10升级后(但不在沙盒环境中)使其在生产中工作。

Just noticed that you're registering the delegate later than application(_:willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions:) methods are called. 刚刚注意到你在application(_:willFinishLaunchingWithOptions:)之后注册了委托application(_:willFinishLaunchingWithOptions:)或者调用了application(_:didFinishLaunchingWithOptions:)方法。

Perhaps this is why your delegate methods are not being hit? 也许这就是为什么你的委托方法没有被击中?

See the docs : 查看文档

Important 重要

You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching. 您必须在应用程序完成启动之前将您的委托对象分配给UNUserNotificationCenter对象。 For example, in an iOS app, you must assign it in the application( :willFinishLaunchingWithOptions:) or application( :didFinishLaunchingWithOptions:) method. 例如,在iOS应用程序中,您必须在应用程序( :willFinishLaunchingWithOptions :)或application( :didFinishLaunchingWithOptions :)方法中分配它。

Going to answer this, maybe it will be useful for other. 要回答这个问题,也许对其他人有用。 My token was wrong. 我的令牌错了。 The encoding has changed with recent updates. 最近的更新编码已更改。 I did actually search all over stack overflow for this and it was to my understanding that I had to convert the Data to a String using the base64string . 我实际上搜索了所有堆栈溢出,这是我的理解,我必须使用base64stringData转换为String However I noticed this string had a few odd characters in it such as \\ . 但是我注意到这个字符串中有一些奇怪的字符,例如\\ I eventually ended up making a Data extension: 我最终最终制作了一个Data扩展:

extension Data {

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

}

This converts the Data to the correct format. 这会将Data转换为正确的格式。 So if this is how you do your conversion, plus all the entitlements are set, you remember to add the new methods: 因此,如果这是您进行转换的方式,并且设置了所有权利,您还记得添加新方法:

@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!")
}

and set the delegate object in didFinishLaunchingWithOptions everything should be good to go. 并在didFinishLaunchingWithOptions设置委托对象应该是好的去。

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

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