在Xamarin Forms Android中使用userId注册到通知中心的异常

问题描述 投票:1回答:1

[我正在开发一个应用程序,而我需要在用户登录时通知用户。当我在登录时调用注册时,我得到一个异常。下面是我的代码和异常。

[服务][IntentFilter(new [] {“ com.google.firebase.INSTANCE_ID_EVENT”})]

public class PushNotificationServiceAndroid : FirebaseInstanceIdService, IPushNotificationService
{
    const string TAG = "MyFirebaseIIDService";
    NotificationHub hub;

    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;

        NotificationHubConstants.Hub = new NotificationHub(AppConstants.NotificationHubName,
                              AppConstants.ListenConnectionString, this);
    }

    public void RegisterToNotificationHub()
    {
        try
        {
            LoggingManager.Enter("RegisterToNotificationHub");

            var tags = new List<string>() { };

            if (Helpers.ApplicationContext.CurrentLoggedInUserId != 0)
            {
                tags.Add(Helpers.ApplicationContext.CurrentLoggedInUserId.ToString());
            }

            //NotificationHubConstants.Hub.UnregisterAll(FirebaseInstanceId.Instance.Token);

            var regID = NotificationHubConstants.Hub.Register(FirebaseInstanceId.Instance.Token, tags.ToArray());

            LoggingManager.Exit("RegisterToNotificationHub");
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
        }
    }

    public void UnRegisterFromNotificationHub()
    {
        try
        {
            LoggingManager.Enter("UnRegisterFromNotificationHub");

            hub = new NotificationHub(AppConstants.NotificationHubName,
                              AppConstants.ListenConnectionString, this);

            hub.UnregisterAll(FirebaseInstanceId.Instance.Token);

            LoggingManager.Exit("UnRegisterFromNotificationHub");
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
        }
    }
}

[当应用程序打开时,将调用OnTokenRefresh()。一旦用户登录到应用程序,我将通过依赖项服务调用RegisterToNotificationHub()。但是在注册到通知中心时,即位于

var regID = NotificationHubConstants.Hub.Register(FirebaseInstanceId.Instance.Token, tags.ToArray());

我收到以下异常。

Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <7f42d9b804da4869b3155f4a330679c7>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <2648c88210c943a888f6191db8d679d6>:0 
  at Android.Runtime.JNIEnv.CallObjectMethod (System.IntPtr jobject, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in <871a122d80384347bfb5f33e1dee9682>:0 
  at WindowsAzure.Messaging.NotificationHub.Register (System.String pnsHandle, System.String[] tags) [0x00081] in <15e1a3139a484a5a85c0680e5d11bb86>:0 
  at BusinessViewChat.Droid.Dependencies.PushNotificationServiceAndroid.RegisterToNotificationHub () [0x00035] in E:\SourceTree\BusinessView\sourcecode\BusinessView\BusinessViewChat\BusinessViewChat.Android\Dependencies\PushNotificationServiceAndroid.cs:46 
  --- End of managed Java.Lang.RuntimeException stack trace ---
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:252)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:219)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:178)
    at com.microsoft.windowsazure.messaging.Connection.executeRequest(Connection.java:134)
    at com.microsoft.windowsazure.messaging.NotificationHub.refreshRegistrationInformation(NotificationHub.java:296)
    at com.microsoft.windowsazure.messaging.NotificationHub.registerInternal(NotificationHub.java:390)
    at com.microsoft.windowsazure.messaging.NotificationHub.register(NotificationHub.java:143)
    at mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5019)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮助我解决问题。谢谢!

xamarin.forms push-notification xamarin.android azure-notificationhub
1个回答
0
投票

它必须在application标记下添加到清单文件中。下一个:

© www.soinside.com 2019 - 2024. All rights reserved.