Push notification 使用Xamarin和Azure App Services移动应用程序的iOS/APN的Azure通知中心InvalidToken错误

Push notification 使用Xamarin和Azure App Services移动应用程序的iOS/APN的Azure通知中心InvalidToken错误,push-notification,xamarin.ios,apple-push-notifications,azure-mobile-services,azure-notificationhub,Push Notification,Xamarin.ios,Apple Push Notifications,Azure Mobile Services,Azure Notificationhub,我正在尝试将通知添加到我的Azure移动应用程序(Azure应用程序服务移动应用程序)中,并且正在努力让它们与iOS一起工作。我已经成功地让他们为Android工作 我已按照联机说明设置Apple证书和资源调配配置文件: iOS应用程序正在向APN注册,并收到一个设备令牌。然后,我删除“”字符和空格,并将注册发送到通知中心 var templates = new JObject { ["genericMessage"] = new JObject {

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

我已按照联机说明设置Apple证书和资源调配配置文件:

iOS应用程序正在向APN注册,并收到一个设备令牌。然后,我删除“”字符和空格,并将注册发送到通知中心

 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);
我已经成功地通过使用RESTAPI将安装置于客户端日志中列出的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)\"}}"
    }
  }
}

事实证明,问题非常简单:

我是这样打注册码的:

 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);
var-token=DeviceToken.Description.Trim('').Replace('','').toupper不变量();
var templates=newjobject
{
[“genericMessage”]=新作业对象
{
{“body”、{\“aps\”:{\“alert\”:\“$(message)\“}}}
}
};
var push=m_mobileseserviceclient.GetPush();
等待push.RegisterAsync(令牌、模板);
其中
DeviceToken
属于
NSData

这意味着我正在将一个
字符串
传递到
RegisterAsync
的第一个参数中,但它需要一个
NSData
类型。必须存在从
string
NSData
的隐式强制转换,因此它仍然编译并运行,但给出了错误的值


修复方法是删除字符串处理并将其保留为
NSData
类型。我发现
注册表同步

问题变得非常简单:

我是这样打注册码的:

 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);
var-token=DeviceToken.Description.Trim('').Replace('','').toupper不变量();
var templates=newjobject
{
[“genericMessage”]=新作业对象
{
{“body”、{\“aps\”:{\“alert\”:\“$(message)\“}}}
}
};
var push=m_mobileseserviceclient.GetPush();
等待push.RegisterAsync(令牌、模板);
其中
DeviceToken
属于
NSData

这意味着我正在将一个
字符串
传递到
RegisterAsync
的第一个参数中,但它需要一个
NSData
类型。必须存在从
string
NSData
的隐式强制转换,因此它仍然编译并运行,但给出了错误的值


修复方法是删除字符串处理并将其保留为
NSData
类型。我发现
注册表同步

请检查您是否已将先前创建的捆绑包ID与新的应用程序ID绑定,并将FinishedLaunching事件重写为。@Amor MSFT是的,捆绑包ID与项目中的捆绑包ID和Apple Developer portal中的应用程序ID匹配。我已经完成了调用APNS启动设置的启动,完全如该链接中所列。我已打开调试跟踪,可以看到从APNS返回的设备令牌,并看到我已将其提交到通知中心。请检查您是否已为该应用程序设置令牌?@Amor MSFT Yes,已创建设置配置文件并链接到启用推送通知(带证书)的应用程序ID。配置文件在Mac电脑上以X代码列出,PC上的Visual Studio已连接到Mac电脑,我已在项目属性中选择了配置文件-请参阅我问题中的屏幕截图。@Amor MSFT我已找到它无法工作的原因-DeviceToken正在被更改(完全!),因此在NotificationHubs中的注册是错误的!请检查您是否已将先前创建的捆绑包ID与新的应用程序ID绑定,并将FinishedLaunching事件重写为。@Amor MSFT是的,捆绑包ID与项目中的捆绑包ID和Apple Developer portal中的应用程序ID匹配。我已经完成了调用APNS启动设置的启动,完全如该链接中所列。我已打开调试跟踪,可以看到从APNS返回的设备令牌,并看到我已将其提交到通知中心。请检查您是否已为该应用程序设置令牌?@Amor MSFT Yes,已创建设置配置文件并链接到启用推送通知(带证书)的应用程序ID。配置文件在Mac电脑上以X代码列出,PC上的Visual Studio已连接到Mac电脑,我已在项目属性中选择了配置文件-请参阅我问题中的屏幕截图。@Amor MSFT我已找到它无法工作的原因-DeviceToken正在被更改(完全!),因此在NotificationHubs中的注册是错误的!看起来最新版本的代码不再带括号,除非功能已移到其他位置。看起来最新版本的代码不再带括号,除非功能已移到其他位置。
 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);