1

我们正在尝试使用Azure通知中心通过GCM/Firebase向Xamarin Forms应用程序发送推送通知。尝试向Azure通知中心注册Xamarin应用程序时发生未经授权的异常

这是我们用它来创建在PCL的MobileServiceClient对象的代码:

public App() 
{ 
    mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://namespace.servicebus.windows.net/NotificationHubName", new ModernHttpClient.NativeMessageHandler()); 
} 

据我了解的文件,它不应该的问题,我们没有使用自定义HTTP处理程序,因为我们不需要提供任何额外的标题或类似的东西。我们只是想要通知,我们不需要Azure在这一点上提供身份验证(我们使用Azure Active Directory B2C)。我可能是错误的,因为缺少身份验证头可能会导致未经授权的异常。

这是我们用来尝试Azure的通知中心注册我们的应用程序的代码:

public async void Register(Microsoft.WindowsAzure.MobileServices.Push push, IEnumerable<string> tags) 
{ 
    try 
    { 
     const string templateBodyGCM = "{\"data\":{\"message\":\"$(messageParam)\"}}"; 

     JObject templates = new JObject(); 
     templates["genericMessage"] = new JObject 
     { 
      {"body", templateBodyGCM} 
     }; 

     await push.RegisterAsync(RegistrationID, templates); 
     Log.Info("Push Installation Id", push.InstallationId.ToString()); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine(ex.Message); 
     Debugger.Break(); 
    } 
} 

不幸的是,一个异常被抛出等待push.RegisterAsync(RegistrationID,模板):

{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed. (Unauthorized) 

我们成功从GCM收到注册ID,并在尝试向Azure注册我们的应用程序以接收推送通知时得到异常。

不幸的是,有关Notification Hub的大多数示例使用的旧代码不需要MobileServiceClient构造函数中的第二个参数。该代码基于此存储库:https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzurePush

在此先感谢您的帮助!

编辑:测试设备上的日期/时间同步处于打开状态。这显然引起了其他开发者的类似问题,但似乎并不是这里的问题。

回答

1

mobileServiceClient =新Microsoft.WindowsAzure.MobileServices.MobileServiceClient( “https://namespace.servicebus.windows.net/NotificationHubName”,新ModernHttpClient.NativeMessageHandler());

根据您的描述和错误,我发现您已将错误的url参数传递给MobileServiceClient对象。

Microsoft.WindowsAzure.MobileServices.MobileServiceClient的网址是您的移动应用的网址,而不是通知中心网址。

象下面这样:

mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("http://yourmobileappname.azurewebsites.net", new ModernHttpClient.NativeMessageHandler()); 

后您已设置移动业务门户通知枢纽的连接,湛蓝的移动SDK会自动可根据权MobileServiceClient得到推对象。

我建议你也可以检查你的移动应用程序如下您已连接到集线器的通知:

enter image description here

+0

谢谢你,固定它 - 我们实际上没有为应用服务Azure中的移动应用程序,因为我们只是想要通知。我们现在创建了一个,并将它与通知中心连接起来,现在通知很好地到达了。再次感谢! – Philippe

+0

非常感谢你,这个伎俩。 –

相关问题