将Channel Uri发送到云服务

时间:2015-03-09 09:20:49

标签: c# windows-phone-8 push-notification

我正在开发一个应用程序以接收推送通知。通过使用下面的代码,我获得了通道uri ..现在我的问题是我应该如何将此发送到我正在使用的webservice ..

    public MainPage()
        {

           HttpNotificationChannel pushChannel;

        // The name of our push channel.
        string channelName = "ToastSampleChannel";

        InitializeComponent();

        // Try to find the push channel.
        pushChannel = HttpNotificationChannel.Find(channelName);

        // If the channel was not found, then create a new connection to the push service.
        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.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

            pushChannel.Open();

            // Bind this new channel for toast events.
            pushChannel.BindToShellToast();

        }
        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);
            // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
            System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
            MessageBox.Show(String.Format("Channel Uri is {0}",
                        pushChannel.ChannelUri.ToString()));


        }
    }



    /// <summary>
    /// Event handler for when the push channel Uri is updated.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {

        Dispatcher.BeginInvoke(() =>
        {
            // Display the new URI for testing purposes.   Normally, the URI would be passed back to your web service at this point.
            System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
            MessageBox.Show(String.Format("Channel Uri is {0}",
                e.ChannelUri.ToString()));

        });
    }

   void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        // 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))
                );
    }

    /// <summary>
    /// Event handler for when a toast notification arrives while your application is running.  
    /// The toast will not display if your application is running so you must add this
    /// event handler if you want to do something with the toast notification.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
    {
        StringBuilder message = new StringBuilder();
        string relativeUri = string.Empty;

        message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString());

        // Parse out the information that was part of the message.
        foreach (string key in e.Collection.Keys)
        {
            message.AppendFormat("{0}: {1}\n", key, e.Collection[key]);

            if (string.Compare(
                key,
                "wp:Param",
                System.Globalization.CultureInfo.InvariantCulture,
                System.Globalization.CompareOptions.IgnoreCase) == 0)
            {
                relativeUri = e.Collection[key];
            }
        }

        // Display a dialog of all the fields in the toast.
        Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString()));

    }

收到Channel uri之后我应该如何将它传递给webserver。我使用了以下代码但没有工作。

private void RegisterUriWithService()
        {
          string baseUri = "http://localhost:8000/RegirstatorService/Register?uri={0}"; 

       string theUri = String.Format(baseUri, pushChannel.ChannelUri.ToString());
        System.Diagnostics.Debug.WriteLine(theUri);
        WebClient client = new WebClient();
        client.DownloadStringCompleted += (s, e) =>
        {
            if (null == e.Error)
                Dispatcher.BeginInvoke(() => UpdateStatus("Registration succeeded"));
            else
                Dispatcher.BeginInvoke(() =>
                                UpdateStatus("Registration failed: " + e.Error.Message));
        };
        client.DownloadStringAsync(new Uri(theUri));
    }

错误就像

一样

错误:无法将lambda表达式转换为&#39; System.Delegate&#39;因为它不是委托类型。 错误:名称&#39; pushChannel&#39;在当前背景下不存在。

控制不在RegisterUriWithService()函数内部。如何克服此错误并将通道uri发送到webservice。 有人请帮帮我.. 很多人提前感谢......

1 个答案:

答案 0 :(得分:0)

不会从代码中的任何位置调用RegisterUriWithService()。

像这样重写你的方法

    private void RegisterUriWithService(string pushChannelUri)
    {
      string baseUri = "http://localhost:8000/RegirstatorService/Register?uri={0}"; 

   string theUri = String.Format(baseUri, pushChannelUri); // pushChannelUri is passed as parameter to this method
    System.Diagnostics.Debug.WriteLine(theUri);
    WebClient client = new WebClient();
    client.DownloadStringCompleted += (s, e) =>
    {
        if (null == e.Error)
            Dispatcher.BeginInvoke(() => UpdateStatus("Registration succeeded"));
        else
            Dispatcher.BeginInvoke(() =>
                            UpdateStatus("Registration failed: " + e.Error.Message));
    };
    client.DownloadStringAsync(new Uri(theUri));
    }

当你的推送频道被打开并且频道URI被更新时,将触发PushChannel_ChannelUriUpdated事件。

将推送渠道URI存储在本地变量 pushChannelUri CALL THE METHOD RegisterUriWithService(pushChannelUri)中。

如果推送通道已存在URI,则控制转到else块

       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);
        // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
        System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
        MessageBox.Show(String.Format("Channel Uri is {0}",
                    pushChannel.ChannelUri.ToString()));

       pushChannelUri = pushChannel.ChannelUri.ToString();
       }

从此块中调用方法 RegisterUriWithService(pushChannelUri)。