Adding a Tag to a NotificationHub in OnRegistered method

扶醉桌前 提交于 2019-12-11 05:12:58

问题


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?


回答1:


Use a Task.Run to get the registration process off the main UI thread.

await Task.Run(() =>
{
   // your register code here...
});


来源:https://stackoverflow.com/questions/38589721/adding-a-tag-to-a-notificationhub-in-onregistered-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!