我們正在嘗試使用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
在此先感謝您的幫助!
編輯:測試設備上的日期/時間同步處於打開狀態。這顯然引起了其他開發者的類似問題,但似乎並不是這裏的問題。
謝謝你,固定它 - 我們實際上沒有爲應用服務Azure中的移動應用程序,因爲我們只是想要通知。我們現在創建了一個,並將它與通知中心連接起來,現在通知很好地到達了。再次感謝! – Philippe
非常感謝你,這個伎倆。 –