In my application after successfully login I would like to register to the NotificationHub with a Tag, where Tag is an email address.
The following scenario works fine on iOS:
MessagingCenter.Subscribe<LoginViewModel, string>(this, MessagesId.RegisterForPush, (s, arg) =>
{
NSSet tags = new NSSet(arg); // create tags if you want
Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
{
if (errorCallback != null)
Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
});
});
but equivalent for Android throws an NetworkOnMainThreadException
:
MessagingCenter.Subscribe<LoginViewModel, string>(this, MessagesId.RegisterForPush, (s, arg) =>
{
var tags = new List<string>() { arg };
try
{
var hubRegistration = Hub.Register(registrationId, tags.ToArray());
}
catch (Exception ex)
{
Log.Error(PushHandlerBroadcastReceiver.TAG, ex.Message);
}
});
Do you have any idea how to solve this issue?
Use a Task.Run
to get the registration process off the main UI thread.
await Task.Run(() =>
{
// your register code here...
});