Dattatray Deokar
Dattatray Deokar

Reputation: 2103

Push notification not received, windows universal 8.1

HI i have implemented push notification in my application, serer is sending notification but not receiving at my end.

          void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
        {
            string typeString = String.Empty;
            string notificationContent = String.Empty;
            switch (e.NotificationType)
            {
                case PushNotificationType.Badge:
                    typeString = "Badge";
                    notificationContent = e.BadgeNotification.Content.GetXml();
                    break;
                case PushNotificationType.Tile:
                    notificationContent = e.TileNotification.Content.GetXml();
                    typeString = "Tile";
                    break;
                case PushNotificationType.Toast:
                    notificationContent = e.ToastNotification.Content.GetXml();
                    typeString = "Toast";
                    // Setting the cancel property prevents the notification from being delivered. It's especially important to do this for toasts:
                    // if your application is already on the screen, there's no need to display a toast from push notifications.
                    e.Cancel = true;
                    break;
                case PushNotificationType.Raw:
                    notificationContent = e.RawNotification.Content;
                    typeString = "Raw";
                    break;
            }

            //   string text = "Received a " + typeString + " notification, containing: " + notificationContent;
            var ignored = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                // rootPage.NotifyUser(text, NotifyType.StatusMessage);

                if (typeString == "Toast")
                {
                    PushNotificationHelper.AddTostNotification(0, notificationContent);
                }
                else if (typeString == "Badge")
                {
                    PushNotificationHelper.AddBadgeNotification(0, notificationContent);
                }

            });
        }

        public async void InitChannel()
        {
            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

            try
            {
                var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

                if (channel != null)
                {

                    //String existingChannel = (String)roamingSettings.Values["ExistingPushChannel"];
                    roamingSettings.Values["ExistingPushChannel"] = channel.Uri;

                    dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                    channel.PushNotificationReceived += OnPushNotificationReceived;

                }
                else
                {
                    roamingSettings.Values["ExistingPushChannel"] = "Failed to create channel";
                }
            }
            catch
            {
                roamingSettings.Values["ExistingPushChannel"] = "Failed to create channel";
            }
        }

        public async void InitNotificationsAsync()
        {
            try
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                String existingChannel = (String)roamingSettings.Values["ExistingPushChannel"];

                string tempDevelopmentKey = "";
                string Platform = "";

                List<string> arrayTags = new List<string>();

#if WINDOWS_APP
                Platform = "windows-tablet";
             tempDevelopmentKey = "dev_WindowsTabletNotification";
#endif

#if WINDOWS_PHONE_APP
            Platform = "windows-phone";
            tempDevelopmentKey ="dev_WindowsPhoneNotification";
#endif

                arrayTags.Add(Platform) ;
                arrayTags.Add(tempDevelopmentKey) ;

                string TMBNotification = (string)roamingSettings.Values["TMBNotification"];
                if(TMBNotification != null)
                {
                    if(TMBNotification == "on")
                    {
                        arrayTags.Add("TMB");
                    }
                }
                string TRSNotification = (string)roamingSettings.Values["TRSNotification"];
                if (TRSNotification != null)
                {
                    if (TRSNotification == "on")
                    {
                        arrayTags.Add("TRS");
                    }
                }
                string IMNotification = (string)roamingSettings.Values["IMNotification"];
                if (IMNotification != null)
                {
                    if (IMNotification == "on")
                    {
                        arrayTags.Add("IM");
                    }
                }
                string SWSNotification = (string)roamingSettings.Values["SWSNotification"];
                if (SWSNotification != null)
                {
                    if (SWSNotification == "on")
                    {
                        arrayTags.Add("ANC");
                    }
                }
                string VIDNotification = (string)roamingSettings.Values["VIDNotification"];
                if (VIDNotification != null)
                {
                    if (VIDNotification == "on")
                    {
                        arrayTags.Add("videos");
                    }
                }


                var hub = new NotificationHub("hubname", "endpoint");
                var result = await hub.RegisterNativeAsync(existingChannel, arrayTags);

                // Displays the registration ID so you know it was successful
                if (result.RegistrationId != null)
                {

                }

            }catch
            {

            }
        }

So how can i confirm that there is no issue in my implementation or there is issue from server end.

Upvotes: 0

Views: 286

Answers (1)

loop
loop

Reputation: 9242

There are couple of steps you can take to debug this problem:-

  • There is broadcast and sending notification, using notification hub, option available on azure portal(you can do from VS also from left side server explorer). When you did this there is a log that will show you whether a notifications sent successfully or not.
  • First just delete all your registrations with notification hub and for very new registration check is your device getting registered with correct tags/channel uri or not(this you cad do from server explorer too)
  • Make sure you are sending/registering the correct templates that are corresponding to WNS service.
  • make sure you are using WNS service this different from what is for WP8 silverlight one.

    ServerExplorer

You can see the errors just at the bottom of this page if any. There is option for device registrations from where you can alter the registrations.

  • DebugNotificationHUb

Upvotes: 2

Related Questions