本文介绍了在OnRegistered方法中将标签添加到NotificationHub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在成功登录后的应用程序中,我想使用一个标签(其中Tag是电子邮件地址)注册到NotificationHub.
In my application after successfully login I would like to register to the NotificationHub with a Tag, where Tag is an email address.
以下情况在iOS上可以正常运行:
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());
});
});
但等同于Android的抛出NetworkOnMainThreadException
:
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?
推荐答案
使用Task.Run
进行主UI线程 off 的注册过程.
Use a Task.Run
to get the registration process off the main UI thread.
await Task.Run(() =>
{
// your register code here...
});
这篇关于在OnRegistered方法中将标签添加到NotificationHub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!