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

非常感謝你,這個伎倆。 –

相關問題