简体   繁体   English

某些注册似乎未在通知中心注册

[英]Some registrations don't seem to be registered in Notification Hub

I'm trying to integrate my Android and iOS native apps with Notification Hub.我正在尝试将我的 Android 和 iOS 本机应用程序与通知中心集成。 For example, on iOS, as soon as I receive the deviceToken and if my user is already authenticated, I register directly with NotificationHub:例如,在 iOS 上,一旦我收到deviceToken并且我的用户已经通过身份验证,我就会直接向 NotificationHub 注册:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    log.debug("Received device token \(deviceToken.hexEncodedString()). Saving device token to user defaults.", "AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken")
    let defaults = UserDefaults.standard
    defaults.set(deviceToken, forKey: UserNotificationDeviceTokenKey)
    defaults.synchronize()

    if let authenticatedUser = Session.shared.authenticatedUser {
        log.debug("Trying to register device token \(deviceToken.hexEncodedString()) to user \(authenticatedUser.id) on notification hub with hub name \(String(describing: hubName)) and connection string \(String(describing: connectionString)). Notification hub null? \(notificationHub == nil)")
        self.notificationHub?.registerNative(withDeviceToken: deviceToken, tags: Set<String>([authenticatedUser.id]), completion: { error in
            if (error != nil) {
                log.error("Error registering for notifications: \(error.debugDescription)", "AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken");
            } else {
                log.debug("Successfully registered device token \(deviceToken.hexEncodedString()) for user with ID \(authenticatedUser.id) and email \(authenticatedUser.emailAddress)", "AppDelegate.didRegisterForRemoteNotificationsWithDeviceToken")
            }
        })
    }
}

And whatever happens, I start by saving the device token to the user defaults so that when a user does log in, he can retrieve the deviceToken from user defaults and call the same registerNative method to create a registration associating this deviceToken with a tag that is the user's identifier:不管发生什么,我首先将设备令牌保存到用户默认值,这样当用户登录时,他可以从用户默认值中检索deviceToken并调用相同的registerNative方法来创建一个注册,将这个deviceToken与一个标签相关联用户的标识符:

func userDidSignIn(user: User) {
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
        let deviceToken = UserDefaults.standard.data(forKey: UserNotificationDeviceTokenKey){

        log.debug("Trying to register device token \(deviceToken.hexEncodedString()) to user \(user.id) on notification hub with hub name \(String(describing: appDelegate.hubName)) and connection string \(String(describing: appDelegate.connectionString)). Notification hub null? \(appDelegate.notificationHub == nil)")

        appDelegate.notificationHub?.registerNative(withDeviceToken: deviceToken, tags: Set<String>([user.id]), completion: { error in
            if (error != nil) {
                log.error("Error registering for notifications: \(error.debugDescription)");
            } else {
                log.debug("Successfully registered device token \(deviceToken.hexEncodedString()) for user with ID \(user.id) (\(user.emailAddress)) who just logged in", context: "HTRootViewController.userDidSignIn")
            }
        })
    } else {
        log.warning("No device token found in settings.", "HTRootViewController.userDidSignIn")
    }
}

I have all my logs remotely sent to Bugfender, which lets me check the logs for all the devices using this app installed in TestFlight, so I can see that all the registerNative's are successful:我将所有日志远程发送到 Bugfender,这让我可以使用安装在 TestFlight 中的此应用程序检查所有设备的日志,因此我可以看到所有 registerNative 都成功:

Trying to register device token 0fea9a4d99ec37dc4f3ac252c35fa4e1617004fd14740973d80a7dfdaeacc857 to user 77bfb1c6-b05a-440b-a7a0-71ae5a91bbb2 on notification hub with hub name Optional("[my notification hub name]") and connection string Optional("[my notification hub connection string]").尝试将设备令牌 0fea9a4d99ec37dc4f3ac252c35fa4e1617004fd14740973d80a7dfdaeacc857 注册到用户 77bfb1c6-b05a-440b-a7a0-71ae5a91。“通知集线器名称”和“通知集线器”连接名称“通知集线器”连接集线器选项名称“通知集线器” Notification hub null?通知中心为空? false错误的

Successfully registered device token 0fea9a4d99ec37dc4f3ac252c35fa4e1617004fd14740973d80a7dfdaeacc857 for user with ID 77bfb1c6-b05a-440b-a7a0-71ae5a91bbb2 ([my user's email]) who just logged in已成功为 ID 为 77bfb1c6-b05a-440b-a7a0-71ae25a91 的用户注册设备令牌 0fea9a4f3ac252c35fa4e1617004fd14740973d80a7dfdaeacc857(刚刚登录的用户)

But then, when I load the list of all the registrations using this .NET C# code:但是,当我使用这个 .NET C# 代码加载所有注册的列表时:

public async Task<List<RegistrationDescription>> GetRegistrations()
{
    var hub = NotificationHubClient.CreateClientFromConnectionString([my notification hub connection string], [my notification hub name]);
    var result = await hub.GetAllRegistrationsAsync(1000);
    return result.ToList();
}

I can see that some registrations are there, but others are nowhere to be found, even though they succeeded according to my mobile logs.我可以看到一些注册在那里,但其他注册无处可寻,即使根据我的移动日志它们成功了。

Does anyone know what else could be going on?有谁知道还会发生什么? What else can I check?我还能检查什么? Why else would registrations not be saved even though the call to registerNative seems to succeed?即使对registerNative的调用似乎成功了,为什么注册也不会被保存?

EDIT: As a matter of fact, and after checking more precisely what is happening, the registration first appears in the list of registrations returned by Notification Hub, but as soon as we try to send a notification, it's as if Notification Hub could not reach the corresponding device token and deleted the registration altogether.编辑:事实上,在更准确地检查发生了什么之后,注册首先出现在通知中心返回的注册列表中,但是一旦我们尝试发送通知,就好像通知中心无法到达相应的设备令牌并完全删除了注册。 Is there a way to see the logs of Notification Hub to see what is going on and why it decides to delete this registration?有没有办法查看通知中心的日志以了解正在发生的事情以及它决定删除此注册的原因?

I figured it out.我想到了。 It turns out it was working on my device because I ran the beta in debug mode, so it was getting its device token from the Sandbox APS environment, which matched the Sandbox endpoint configured in Notification Hub, but other users were using the Production APS environment configured in the archive build, so they got device tokens for a different environment and notification hub correctly identified as incorrect device tokens.事实证明它在我的设备上运行,因为我在调试模式下运行测试版,所以它从沙箱 APS 环境中获取其设备令牌,这与通知中心配置的沙箱端点相匹配,但其他用户正在使用生产 APS 环境在存档版本中配置,因此他们获得了不同环境的设备令牌,并且通知中心被正确识别为不正确的设备令牌。 Once we switched the Notification Hub to the production APNS endpoint, all the TestFlight users were able to receive notifications because their registrations stayed in Notification Hub.一旦我们将通知中心切换到生产 APNS 端点,所有 TestFlight 用户都能够收到通知,因为他们的注册保留在通知中心。 Now that does mean that my debug builds trying to use that environments won't receive notifications, but that's OK, that's why we have a different environment and Notification Hub for development purposes.现在这确实意味着我尝试使用该环境的调试版本不会收到通知,但没关系,这就是为什么我们有不同的环境和通知中心用于开发目的。 So if anyone sees something similar, don't forget to double check your Notification Hub configuration matches the type of build your users are using, and don't forget that TestFlight builds are now production-type builds, same as App Store builds.因此,如果有人看到类似的内容,请不要忘记仔细检查您的通知中心配置是否与您的用户正在使用的构建类型相匹配,并且不要忘记 TestFlight 构建现在是生产类型构建,与 App Store 构建相同。

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

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