Search code examples
xamarinpush-notificationxamarin.androidpushy

PushyNetworkException thrown on Pushy.Register


I am trying to get pushy from pushy.me to work with Xamarin. I have created a Bindings Library for Pushy and it builds fine. On Pushy.Register(context) I get an exception thrown. I can't figure out what is causing the exception has anyone else tried to implement this into xamarin and were successful or have any ideas what could be going wrong.

The eception is thrown on

JNIEnv.GetString(JNIEnv.CallStaticObjectMethod(class_ref,id_register_Landroid_content_Context_, __args),JniHandleOwnership.TransferLocalRef);

Thanks

ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException: android.os.NetworkOnMainThreadException
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00082] in /Users/builder/data/lanes/3511/ce955cc0/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12649 
at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00000] in /Users/builder/data/lanes/3511/ce955cc0/source/monodroid/src/Mono.Android/JNIEnv.g.cs:562 
at ME.Pushy.Sdk.Pushy.Register (Android.Content.Context p0) [0x00047] in E:\GitHub\ugs-mobile-app\PushyBindingsLibrary\obj\Debug\generated\src\ME.Pushy.Sdk.Pushy.cs:98 
--- End of managed ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException stack trace ---
me.pushy.sdk.util.exceptions.PushyNetworkException: android.os.NetworkOnMainThreadException
at me.pushy.sdk.util.PushyHTTP.post(PushyHTTP.java:61)
at me.pushy.sdk.Pushy.register(Pushy.java:126)
at md52b714a6cf9c1cb241b63b6417cd05e8f.SplashActivity.n_onCreate(Native Method)
at md52b714a6cf9c1cb241b63b6417cd05e8f.SplashActivity.onCreate(SplashActivity.java:30)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)


static IntPtr id_register_Landroid_content_Context_;
// Metadata.xml XPath method reference: path="/api/package[@name='me.pushy.sdk']/class[@name='Pushy']/method[@name='register' and count(parameter)=1 and parameter[1][@type='android.content.Context']]"
[Register ("register", "(Landroid/content/Context;)Ljava/lang/String;", "")]
public static unsafe string Register (global::Android.Content.Context p0)
{
    if (id_register_Landroid_content_Context_ == IntPtr.Zero)
        id_register_Landroid_content_Context_ = JNIEnv.GetStaticMethodID (class_ref, "register", "(Landroid/content/Context;)Ljava/lang/String;");
    try
    {
        JValue* __args = stackalloc JValue[1];
        __args[0] = new JValue(p0);
        string __ret =JNIEnv.GetString(JNIEnv.CallStaticObjectMethod(class_ref,id_register_Landroid_content_Context_, __args),JniHandleOwnership.TransferLocalRef);
        return __ret;
    }
    catch (SystemException e)
    {
        return null;
    }
    catch (ME.Pushy.Sdk.Util.Exceptions.PushyNetworkException e)
    {
        return null;
    }
    finally
    {
    }
}

Solution

  • android.os.NetworkOnMainThreadException

    Network operations on Android need to be performed off the main UI thread, the easiest way is use a Task to push it onto a thread in the default threadpool:

    await Task.Run(() =>
    {
        Pushy.Register(context);
    });
    

    Task.Run: Queues the specified work to run on the ThreadPool and returns a task handle for that work.

    Ref: https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx