【问题标题】:Azure Notification Hub - "The token obtained from the token provider is wrong"Azure 通知中心 - “从令牌提供程序获取的令牌错误”
【发布时间】:2019-05-30 10:10:07
【问题描述】:

使用 Notifications REST API 和 JavaScript,我们通过 FCM 订阅我们的 Progressive Web App,然后调用registrations 端点在我们的 ANH 上注册

注册完成正常,我们可以在我们的集线器上使用正确的平台和填充的 PNS 标识符看到注册

当我们尝试向所有注册设备发送测试消息时,我们在 ANH 中收到以下错误

从令牌提供者获取的令牌错误

我们已尝试发送 Firebase 返回的整个 endpoint 对象,仅发送 subscriptionId 和其他各种组合

错误信息是否意味着我们使用了错误的密钥对进行了订阅,或者令牌的格式不正确?在注册端点注册时,没有任何地方显示GcmRegistrationId 格式的示例

【问题讨论】:

标签: firebase google-cloud-messaging firebase-cloud-messaging progressive-web-apps azure-notificationhub


【解决方案1】:

您可以使用以下方法注册您的设备:

如果是 GCM,请遵循以下方法:

将 Nuget 包用于通知中心。

对于 DeviceRegistration.cs

 public class DeviceRegistration
    {
        public string Platform { get; set; } 
        public string Handle { get; set; }
        public string[] Tags { get; set; }
    }

对于 NotificationClient.cs

 using Microsoft.Azure.NotificationHubs; // Namespace to be used

// Use below method to get registrationID

 public async Task<string> GetRegistrationID(NotificationHubClient Hub, string handle = null)
        {
            string newRegistrationId = null;

            // make sure there are no existing registrations for this push handle (used for iOS and Android)
            if (handle != null)
            {
                var registrations = await Hub.GetRegistrationsByChannelAsync(handle, 100);
                foreach (RegistrationDescription registration in registrations)
                {
                    if (newRegistrationId == null)
                    {
                        newRegistrationId = registration.RegistrationId;
                    }
                    else
                    {
                        await Hub.DeleteRegistrationAsync(registration);
                    }
                }
            }

            if (newRegistrationId == null)
                newRegistrationId = await Hub.CreateRegistrationIdAsync();

            return newRegistrationId;
        }

// Use below method to upsert registration to azure

 public async Task UpsertRegistration(string registrationid, DeviceRegistration deviceUpdate, NotificationHubClient Hub)
            {
    string[] tags = { "abc","def" }; // These are used to send notifications
     DeviceRegistration deviceRegistration = new DeviceRegistration
                    {
                        Handle = newDeviceToken, // Device token given by Firebase
                        Platform = "gcm", // Specify gcm for android and "apns" for ios  
                        Tags = tags
                    };

                RegistrationDescription registration
                         = new GcmRegistrationDescription(deviceRegistration.Handle);



                registration.RegistrationId = registrationid;

                // add check if user is allowed to add these tags
                registration.Tags = new HashSet<string>();
                foreach (string tag in deviceUpdate.Tags)
                {
                    registration.Tags.Add(tag);
                }

               await Hub.CreateOrUpdateRegistrationAsync(registration);           
            }

【讨论】:

  • 我们仍然收到错误,我们没有使用 Nuget 包,我们使用的是 JavaScript。运行代码后,GcmRegistrationId 的格式是什么? (registration.RegistrationId
  • RegistrationId 是从 Azure 通知中心获得的。我在回答中提到了该方法。注册 ID 示例为“7879195269520841161-4412273044718808258-3”
  • 我不是在谈论 Azure 返回的 registrationId - 我已经有了。当我们尝试发送消息时,Azure 告诉我们令牌(PNS 句柄)不正确,所以我想知道在调用注册 REST API 端点时我应该发送到 Azure 的 PNS 句柄(GcmRegistrationId)的格式是什么
  • PNS 句柄和 GCMRegistrationID 不同。 PNS句柄实际上是凭证,从“谷歌火力”这就好比“eIiZYb98yG8:APA91bFmRozDiXoPkQolbnsFnpGaeRp8TUi_7kcOl2s0W-GavRJW07XDaUPNJlZOScWVsUwLRvxg2fbow45QIE1Pf4-HKBet_vCbdW7zvofkATkDRYrPRcP0AMOd6m2wl7uo1J5WgLD1”接收移动设备。从 Firebase 获得此信息后,您可以调用 azure hub 为您提供我提到的方法的registrationID。获得两者后,您通过上述方法将您的 PNS 句柄和registrationID 注册到集线器为“UpsertRegistration”
  • @Mike 您在这方面需要任何进一步的帮助吗?
猜你喜欢
  • 2017-02-03
  • 2022-08-23
  • 1970-01-01
  • 2021-01-23
  • 2017-12-28
  • 2015-08-27
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多