Search code examples
windows-phone-8push-notificationmpns

wp8 Handle raw push notification in more than one pages


I am developing a wp8 application and the need is the above:

My server sends some raw push notifications that i can handle in my mainpage successful. But i have more pages so i need my app continue to get and handle notifications when the user is on the other pages.

As far i have tried to add the same code as i have put in the main page to handle notifications

  string channelName = "test";
            pushChannel = HttpNotificationChannel.Find(channelName);
            if (pushChannel == null)
            {
                pushChannel = new HttpNotificationChannel(channelName);

                // Register for all the events before attempting to open the channel.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                // Register for this notification only if you need to receive the notifications while your application is running.
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);

                //pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
                pushChannel.Open();
                // Bind this new channel for toast events.
                //pushChannel.BindToShellToast();
                System.Threading.Thread.Sleep(3000);
                channel = pushChannel.ChannelUri.ToString();
                cSettings.device_notify_id = channel;
            }
            else
            {
                // The channel was already open, so just register for all the events.
                pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
                pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);
                // Register for this notification only if you need to receive the notifications while your application is running.
                // pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
                pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
                System.Threading.Thread.Sleep(3000);
                channel = pushChannel.ChannelUri.ToString();
                cSettings.device_notify_id = channel;
                // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
            }

and the handlers methods

void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
    Dispatcher.BeginInvoke(() =>
    {
        cSettings.device_notify_id = e.ChannelUri.ToString();
        // Display the new URI for testing purposes.   Normally, the URI would be passed back to your web service at this point.
    });
}

void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
    cSettings set = new cSettings();
    set.LogEx(new Exception((String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",
            e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))));

    // Error handling logic for your particular application would be here.
    Dispatcher.BeginInvoke(() =>
        MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",
            e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
            );
}

void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
    string message;
    VibrationDevice vibr = VibrationDevice.GetDefault();
    vibr.Vibrate(TimeSpan.FromSeconds(3));




    using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
    {
        message = reader.ReadToEnd();
    }
    cSettings set = new cSettings();
    string n_type = "";
    string n_header = "";
    //var obj = set.parse_stringfromnotify(message, ref n_type, ref n_header);
    Dispatcher.BeginInvoke(() => nofication_received_action(message, n_type, ""));
}


   private void nofication_received_action(string n_header, string n_type, object data)
        {
            MessageBoxResult result;
            CallSrvData cdata = new CallSrvData();
            Exception ex = null;
            WP_MemberData m;
            WP_MemberRules wpmr;
            cSettings set;
            MemberRules mr;
            Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
            Rules c_rules;


                    Notify.data = data;
                    Notify.msg_box_text = String.Format("{0}", n_header);
                    //dose k data sth forma
                    toast = new Microsoft.Phone.Shell.ShellToast();
                    toast.Content = "Invitation received";
                    toast.Title = "Title : ";
                    //SetProperty(toast, "Sound", new Uri("/data/alert.mp3", UriKind.Relative));
                    toast.NavigationUri = new Uri("/forms/Notify.xaml?type=0", UriKind.Relative);
                    toast.Show();
}

When the app is in background and i successfully get the notification and navigate to Notify.xaml the mechanism works fine, but when i go back or i press start button to leave from Notify.xaml and i resend a notification nothing happens. I have tried to add the same code and in Notify.xaml but again nothing happen when i send notification. In comparison with android where you just register listeners once in your app and then you can receive notification in in any page even the app is "closed" who can i succeed something like that or can i succeed something like that?

Thx a lot for your contribution.


Solution

  • I have found that i can create all the notification functionality in a class which will initialize on app.cs or any time is needed.