【问题标题】:Azure Notification Hub InvalidToken error for iOS/APNS using Xamarin and Azure App Services Mobile App使用 Xamarin 和 Azure 应用服务移动应用的 iOS/APNS 的 Azure 通知中心 InvalidToken 错误
【发布时间】:2017-12-16 14:50:50
【问题描述】:

我正在尝试将通知添加到我的 Azure 移动应用程序(Azure 应用程序服务移动应用程序),并且正在努力让它们与 iOS 一起使用。我已成功让他们为 Android 工作。

我已按照在线说明设置 Apple 证书和配置文件:

https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter5/ios/

iOS 应用正在向 APNS 注册并收到设备令牌。然后,我删除了“”字符以及空格,并将注册发送到通知中心。

 var templates = new JObject
 {
     ["genericMessage"] = new JObject
     {
         {"body", "{\"aps\":{\"alert\":\"$(message)\"}}"}
     }
 };

 var push = m_MobileServiceClient.GetPush();
 await push.RegisterAsync(token, templates);

当我发送第一个推送通知请求时...

 var notification = new Dictionary<string, string> { { "message", "Test notification message" } };
 await m_Hub.SendTemplateNotificationAsync(notification);

...注册已从通知中心删除。

我已将 Azure 通知中心的定价层提高到标准,并用它来查找存储在存储帐户中的跟踪,它显示“InvalidToken”作为来自 APNS 的错误。

我做过/检查过的事情:

  • 通知中心作为 APNS 凭据设置为使用开发证书(为启用推送通知的非通配符应用 ID 从 Apple 开发人员门户创建和导出)和沙盒环境。
  • iOS 应用项目设置为使用开发者身份和应用 ID 链接到的供应配置文件:

  • 我在 Info.plist 中添加了以下内容(并非所有说明都包含此内容,但其他文章建议它是必需的)

    <key>aps-environment</key>
    <string>development</string>

我试图解决的问题

  • 尝试将 DeviceToken 大写
  • 尝试 NOT 删除空格和/或 '' 字符(这样做会产生“InvalidTokenSize”错误,这让我相信删除字符 is 必需)
  • 尝试将通知中心更改为使用生产推送通知证书(在更改证书之间删除中心)

我仍然继续得到“InvalidToken”。

有人知道我可能做错了什么吗?

编辑

APNS 的 DeviceToken(删除空格和 '' 字符后)为 64 个字符长。我已经检查过了,这就是我在客户端中获得并发送到通知中心的内容。但是当我查看通知中心时,我得到了一个 128 个字符长的 DeviceToken。

这似乎是问题的原因。这些是我在移动端的日志(我正在输出到输出窗口)

RegisteredForRemoteNotifications 触发

使用 NH 注册:7012D5D766E3D765DE017F65A58E0DFB461A6B779C57B6C660040F6495EA20AB

但这就是随后出现在设备注册中的内容(调用 https://{my_namespace}.servicebus.windows.net/{my_hub}/installations/{installationid}?api-version=2015-01

{
    "installationId": "{installationId}",
    "pushChannel": "37303132443544373636453344373635444530313746363541353845304446423436314136423737394335374236433636303034304636343935454132304142",
    "pushChannelExpired": false,
    "platform": "apns",
    "expirationTime": "9999-12-31T23:59:59.9999999Z",
    "tags": [
        ...
    ],
    "templates": {
        "genericMessage": {
            "body": "{\"aps\":{\"alert\":\"$(message)\"}}",
            "tags": [
                "genericMessage"
            ]
        }
    }
}

我已成功通过使用 REST API 将安装放入客户端日志中列出的 DeviceToken 来使通知成功运行。

如果我重新启动应用程序,当我重新注册到通知中心时,值会变回,所以在调用 NotificationHubClient 后的某个地方,值正在改变!

编辑 2

我已将 httpclient 流量写入输出窗口,这似乎是客户端问题,因为在调用服务器时 DeviceToken 不正确:

Method: PUT, 
RequestUri: 'https://{my_namespace}/push/installations/5ed26cac-2735-4d58-91f4-2220569a3f0c', 
Version: 1.1, 
Content: System.Net.Http.StringContent, 
Headers:
{
X-ZUMO-INSTALLATION-ID: 5ed26cac-2735-4d58-91f4-2220569a3f0c
X-ZUMO-AUTH: ...
Accept: application/json
User-Agent: ZUMO/3.1 (lang=Managed; os=iOS; os_version=10.3.2; arch=MacOSX; version=3.1.50105.0)
X-ZUMO-VERSION: ZUMO/3.1 (lang=Managed; os=iOS; os_version=10.3.2; arch=MacOSX; version=3.1.50105.0)
ZUMO-API-VERSION: 2.0.0
Content-Type: application/json; charset=utf-8
}
{
  "pushChannel": "37303132443544373636453344373635444530313746363541353845304446423436314136423737394335374236433636303034304636343935454132304142",
  "platform": "apns",
  "templates": {
    "genericMessage": {
      "body": "{\"aps\":{\"alert\":\"$(message)\"}}"
    }
  }
}

【问题讨论】:

  • 请检查您是否有 updated the Bundle Identifier 与您之前创建的带有新应用程序 ID 的捆绑包 ID,并将 FinishedLaunching 事件覆盖为 configure NotificationSettings
  • @Amor-MSFT 是的,项目中的 Bundle ID 与 Apple Developer 门户中的 App ID 匹配。我已经 FinishedLaunching 完全按照该链接中列出的方式调用 APNS 启动设置。我打开了调试跟踪,可以看到从 APNS 返回的设备令牌,并看到我将它提交到通知中心。
  • @Amor-MSFT 是的,配置文件已创建并链接到启用了推送通知(带有证书)的应用程序 ID。该配置文件在 Mac 上的 X-Code 中列出,PC 上的 Visual Studio 已连接到 Mac,我在项目属性中选择了配置文件 - 请参阅我的问题中的屏幕截图。
  • @Amor-MSFT 我找到了它不起作用的原因 - DeviceToken 正在被更改(完全!)所以 NotificationHubs 中的注册是错误的!

标签: push-notification xamarin.ios apple-push-notifications azure-mobile-services azure-notificationhub


【解决方案1】:

这个问题原来很简单:

我是这样调用注册码的:

 var token = DeviceToken.Description.Trim('<', '>').Replace(" ", "").ToUpperInvariant();
 var templates = new JObject
 {
     ["genericMessage"] = new JObject
     {
         {"body", "{\"aps\":{\"alert\":\"$(message)\"}}"}
     }
 };

 var push = m_MobileServiceClient.GetPush();
 await push.RegisterAsync(token, templates);

其中DeviceToken 的类型为NSData

这意味着我将string 传递给RegisterAsync 的第一个参数,但它需要NSData 类型。必须有从 stringNSData 的隐式转换,所以它仍然编译并运行,但给出了错误的值。

修复是删除字符串处理并将其保留为NSData 类型。原来RegisterAsyncmethod already does this processing for you!

【讨论】:

  • 看起来最新版本的代码不再去掉括号,除非功能已经转移到其他地方。
猜你喜欢
  • 2017-01-03
  • 2018-04-05
  • 2017-06-08
  • 2016-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
相关资源
最近更新 更多