2013-04-20 138 views
2

我試圖在WP7.1中使用推送通知。我創建了類和一些PHP類。該示例適用於所有通知類型,但我的問題是UriChannel。用我的簡單示例,我必須在php代碼中手動編寫URI通道。推送通知,Uri通道和PHP服務器端

  • 對於安裝了我的應用程序的所有設備,uri通道是否是唯一的?

  • 如何將Uri頻道發送到我的服務器?

謝謝。

這是我的代碼:

/// Holds the push channel that is created or found. 
    HttpNotificationChannel pushChannel; 
    // The name of our push channel. 
    string channelName = "LiveTileChannel"; 

    // Costruttore 
    public MainPage() 
    { 
     InitializeComponent(); 
     CreatePushChannel(); 
    } 

    private void CreatePushChannel() 
    { 
     // 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); 
      pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived); 
      pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushChannel_HttpNotificationReceived); 
      pushChannel.Open(); 
      // Bind this new channel for Tile events. 
      pushChannel.BindToShellTile(); 
      pushChannel.BindToShellToast(); 
      connessioneTxt.Text = "Connessione avvenuta"; 
     } 
     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); 
      pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived); 
      pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushChannel_HttpNotificationReceived); 
      // 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()); 
      uriTxt.Text=pushChannel.ChannelUri.ToString(); 
      connessioneTxt.Text = "Connessione già avvenuta"; 
     } 
    } 

    void pushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e) 
    { 
     string message; 

     using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body)) 
     { 
      message = reader.ReadToEnd(); 
     } 

     Dispatcher.BeginInvoke(() => rawTxt.Text = String.Format("Received Notification {0}:\n{1}", 
       DateTime.Now.ToShortTimeString(), message) 
       ); 

    } 

    /// Event handler for when the Push Channel Uri changes.  
    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()); 
      uriTxt.Text = pushChannel.ChannelUri.ToString(); 
     }); 
    } 

    // Event handler for when a Push Notification error occurs. 
    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) 
    { 
     System.Diagnostics.Debug.WriteLine("notifica aperta"); 
    } 


} 

和樹的PHP例子之一:

Pastebin of PHP

回答

1

的URI通道是在其上安裝了應用程序唯一的每個設備。它必須對每個設備都是唯一的,因爲這是告訴MPN服務哪個設備發送通知。它與Google雲消息傳遞的Apple推送通知和註冊ID的設備令牌具有相同的用途。

您可以通過發送一些HTTP GET或POST請求到您的服務器,將URI Chnnael作爲輸入參數,將URI Channel發送到您的服務器。

下面是comminicating從MSDN採取了你的服務器的一些準則:

您的每一個應用程序啓動的時候,你應該從你推 通道發送出去的推送通知雲服務通過URI。它還建議您將設備ID傳遞到您的雲服務 ,以便雲服務可以跟蹤指定了哪些設備的URI分配給 。如果URI發生變化,那麼雲服務可以替換該設備ID的舊的 URI。 Windows Phone不提供框架以便 這樣做,因爲在大多數情況下,應用程序和雲服務已經有他們自己的協議,它們用於彼此通信。用您的雲服務進行通信

最佳實踐包括:

的應用程序及其相對應的雲服務進行身份驗證。

應用程序應該在將URI發送到其相應的雲服務之前加密其通知通道URI。

如果你的雲服務將使用不會在Windows 手機OS 7.0中存在的通知屬性,那麼你應該在操作系統版本信息傳遞給你的雲服務,使 雲服務能夠正確降級爲Windows手機OS的通知7.0 客戶。

雲服務應驗證從其對應的應用程序接收到的通知通道URI並將其以安全的方式存儲。

當從應用程序啓動會話時,應始終將通知信道URI發送到其相應的雲服務 。

雲服務應該有一個狀態代碼,它可以發送到其相應的應用程序, 將觸發應用程序來創建新的通知通道URI。