2013-03-05 120 views
2

通道URI不工作在這裏的Windows Phone 8模擬器也沒有創造是代碼:推送通知中的Windows Phone 8

`private void OnChannelUriChanged(Uri value) 
    { 
     Dispatcher.BeginInvoke(() => 
     { 
      txtURI.Text = value.ToString(); 
     }); 

     Debug.WriteLine("URI: " + value.ToString()); 
    } 

    private static void BindToShell(HttpNotificationChannel httpChannel) 
    { 
     try 
     { 
      httpChannel.BindToShellToast(); 
     } 
     catch (Exception) 
     { 
     } 
    } 
    void httpChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e) 
    { 
     Dispatcher.BeginInvoke(() => 
     { 
      txtURI.Text = "Toast Notification Message Received: "; 
      if (e.Collection != null) 
      { 
       Dictionary<string, string> collection = (Dictionary<string, string>)e.Collection; 
       System.Text.StringBuilder messageBuilder = new System.Text.StringBuilder(); 
       foreach (string elementName in collection.Keys) 
       { 
        txtURI.Text += string.Format("Key: {0}, Value:{1}\r\n", elementName, collection[elementName]); 
       } 
      } 
     }); 
    } 
    void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) 
    { 
     //You get the new Uri (or maybe it's updated) 
     OnChannelUriChanged(e.ChannelUri); 
    } 

    private void SetupChannel() 
    { 
     HttpNotificationChannel httpChannel = null; 
     string channelName = "DemoChannel"; 

     //if channel exists, retrieve existing channel 
     httpChannel = HttpNotificationChannel.Find(channelName); 

     if (httpChannel != null) 
     { 
      //If we cannot get Channel URI, then close the channel and reopen it 
      if (httpChannel.ChannelUri == null) 
      { 
       httpChannel.UnbindToShellToast(); 
       httpChannel.Close(); 
       SetupChannel(); 
       return; 
      } 
      else 
      { 
       OnChannelUriChanged(httpChannel.ChannelUri); 
      } 
      BindToShell(httpChannel); 
     } 
     else 
     { 
      httpChannel = new HttpNotificationChannel(channelName); 
      httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated); 
      httpChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(httpChannel_ShellToastNotificationReceived); 
      httpChannel.Open(); 
      BindToShell(httpChannel); 
     } 
    } 

    private void btnCreateChannel_Click(object sender, RoutedEventArgs e) 
    { 
     SetupChannel(); 
    }` 

任何人都可以發送一些解決方案來解決這個問題 感謝

+1

嗨仇德,你的代碼出了什麼問題。它是否會給出例外,或者它不起作用到目前爲止您嘗試過的是什麼? – 2013-03-05 10:00:46

+0

謝謝,回覆。我可以運行代碼,但無法創建通道uri。 – Vendetta 2013-03-05 10:47:06

+0

同樣的問題在我身邊 – MansinhDodiya 2013-03-25 04:12:43

回答

0

試試這個一個:

public MainPage() 
    { 
     InitializeComponent(); 
     /// Holds the push channel that is created or found. 
     HttpNotificationChannel pushChannel; 

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

     // 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())); 

     } 

    } 

    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)) 
       ); 
    } 
    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())); 

    } 

    }