2013-08-25 136 views
0

我試圖讓一個原始推送通知,從Azure的移動服務工作,到Windows Phone 8。的Windows Phone 8原始通知使用Windows Azure移動服務

我只使用Windows Azure簽署了自由移動服務隨附20mb免費數據庫和免費移動服務。

管理Windows Azure服務的站點有一個鏈接,指向如何嚮應用程序發送推送通知以更新翻轉塊的示例here

在插入到表中時,發送通知的腳本運行。

MSDN上還有另外一個例子,它提供了一個如何創建一個向WP8應用程序發送原始通知的ASP頁面的例子。那個例子是here

我已經得到了兩個例子的工作,但我需要第一個例子發送原始通知,所以第二個例子中的代碼工作。

這是我的代碼:

在我的Windows Phone 8的應用程序我有這個接收通知,在App.xaml.cs:

private void AcquirePushChannel() 
    { 

     /// Holds the push channel that is created or found. 
     HttpNotificationChannel pushChannel; 

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

     // Try to find the push channel. 
     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); 
      pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived); 

      pushChannel.Open(); 

     } 
     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.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()); 
      //MessageBox.Show(String.Format("Channel Uri is {0}", 
      // pushChannel.ChannelUri.ToString())); 

     } 

    } 

    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) 
    { 

     Deployment.Current.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. 
     Deployment.Current.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 raw notification arrives. For this sample, the raw 
    /// data is simply displayed in a MessageBox. 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e) 
    { 
     string message; 

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


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

在應用程序啓動它調用AcquirePushChannel:

private void Application_Launching(object sender, LaunchingEventArgs e) 
    { 
     AcquirePushChannel(); 

    } 

我的問題是在我的Windows Azure移動服務數據庫,其中我有以下代碼插入表發送原始推送通知,這是行不通的:

function insert(item, user, request) { 

request.execute({ 
    success: function() { 
     // Write to the response and then send the notification in the background 
     request.respond(); 
     // for testing I'm manually putting in the channel ID where it says <channelID> below 
     push.mpns.sendRaw(<channelID>, 
      'test', { 
      success: function (pushResponse) { 
       console.log("Sent push:", pushResponse); 
      } 
     }); 
    } 
}); 

} 

有這個here的文檔,所以我確定它是正確的,但它不起作用。

還有一個例子here

另一個問題是,如何通過Windows Azure查看console.log?

+0

您可以訪問Azure移動服務儀表板的最後一個選項(日誌)上的console.log條目 –

+0

感謝您的幫助。我已經能夠找出一些事情。 – doktorg

回答

0

我能夠從日誌中發現我的代碼沒有發送通知,並發現它是我的測試方法,它導致了它,所以我修復了它。我發現,當我使用的代碼插入腳本只觸發:

private MobileServiceCollection<TodoItem, TodoItem> items; 

private IMobileServiceTable<TodoItem> todoTable = App.MobileService.GetTable<TodoItem>(); 

private async void InsertTodoItem(TodoItem todoItem) 
    { 
     // This code inserts a new TodoItem into the database. When the operation completes 
     // and Mobile Services has assigned an Id, the item is added to the CollectionView 
     await todoTable.InsertAsync(todoItem); 
     items.Add(todoItem); 
    } 

如果您使用Management Studio和手動插入一行例如插入腳本不運行。