Exception caught sending update: System.NullReferenceException: Object reference not set to an insta

SreeeeSreeee INMember ✭✭✭✭✭
edited March 12 in Xamarin.Forms

I added push notification handling codes to my xamarin forms UWP application.

Codes:

async Task InitRemoteNotificationAsync()
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
        if (channel != null)
        {
            channel.PushNotificationReceived += OnPushNotificationReceived;
            Debug.WriteLine($"Received token:{channel.Uri}");
        }
    }

private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
    {

        if (args.ToastNotification.Content.InnerText != null)
        {
            var msg = args.ToastNotification.Content.InnerText;
            Xamarin.Forms.MessagingCenter.Send<object, string>(this, MyProject.App.NotificationReceivedKey, msg);
        }
    }

I have added null checks in my codes. When I try to push a test notification from the http://pushtestserver.azurewebsites.net/wns/ getting the following exception.

Exception caught sending update: System.NullReferenceException: Object reference not set to an instance of an object.
at WebRole1.WNS.WebForm1.PostToWns(String secret, String sid, String uri, String xml, String notificationType, String contentType)
at WebRole1.WNS.WebForm1.btnDiyPush_Click(Object sender, EventArgs e)

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    I used Azure to test notification on UWP, it works properly. Try to precisely follow this tutorial step by step: https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification. Do not forget to associate your app with the store.
    Here is my InitNotificationsAsync using Azure notification hub:

    private async void InitNotificationsAsync()
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    
        var hub = new NotificationHub("<your hub name>", "<Your DefaultListenSharedAccessSignature connection string>");
        var result = await hub.RegisterNativeAsync(channel.Uri);
        channel.PushNotificationReceived += Channel_PushNotificationReceived;
    
        //Displays the registration ID so you know it was successful
        if (result.RegistrationId != null)
        {
            var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
            dialog.Commands.Add(new UICommand("OK"));
            await dialog.ShowAsync();
        }
    }
    

    Moreover, it looks like your issue comes from other parts. Try to remove the OnPushNotificationReceived received method to test your notification.

Sign In or Register to comment.