0

Whenever I am trying to create push notification in windows phone 7 and windows phone 8, my code is working fine but whenever I tried to build same code on windows phone 8.1, it's showing 'System.NullReferenceException' Occur when generate Chanal Uri In Windows Phone 8.1.

Code is given below :

            String channelUri="XYZ";
            HttpNotificationChannel myPushChannel = HttpNotificationChannel.Find(channelUri);
            if (null == myPushChannel)
            {

                    myPushChannel = new HttpNotificationChannel(channelUri);
                    myPushChannel.Open();
                    myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
                    myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
                    myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
                    myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
                    myPushChannel.BindToShellTile();
                    myPushChannel.BindToShellToast();
                    uri = myPushChannel.ChannelUri;
            }
            else
            {

                if (myPushChannel != null)
                {
                    myPushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myPushChannel_ShellToastNotificationReceived);
                    myPushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myPushChannel_ChannelUriUpdated);
                    myPushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myPushChannel_ErrorOccurred);
                    myPushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myPushChannel_HttpNotificationReceived);
                    uri = myPushChannel.ChannelUri;
                    if (!myPushChannel.IsShellToastBound)
                        myPushChannel.BindToShellToast();
                        if (!myPushChannel.IsShellTileBound)
                        myPushChannel.BindToShellTile();
                }

            }
        }

Everything is working fine in windows phone 7/8 but not working on windows phone 8.1.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Oct 30 '15 at 15:45
  • Voting to close again as a duplicate. – halfer Feb 27 '16 at 11:48

1 Answers1

0

If we are talking about WP8.1 Universal Apps, and not silverlight, you should use line below to obtain PushNotificationChannel:

PushNotificationChannel channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
Festyk
  • 316
  • 1
  • 6