2014-02-18 31 views
1

我正在通過節點js腳本使用azure通知中心發送推送通知。我可以發送和接收推送通知。我不知道如何檢索數據。我發送推送如下: -如何從推送通知中獲取數據?

function sendNotifications(pushMessage) { 
    console.log('inside sendNotifications'); 
    var hub = azure.createNotificationHubService('hubname','connection string'); 

    hub.mpns.sendToast(
     null, 
     { 
      text1: pushMessage, 
      text2: 'some data' 
     }, 
     function (error) 
     { 
      if (!error) 
      { 
       //message send successfully 
       console.log("mpns.sendToast push success: "+error); 
       RESPONSE.send(statusCodes.OK, { ResponseMessage : 'mpns.sendToast message success' }); 
      } 
      else 
      { 
       // msg failed to send 
       console.log("errro error.shouldDeleteChannel: "+error); 
       RESPONSE.send(statusCodes.OK, { ResponseMessage :'mpns.sendToast message error '+error }); 
      } 
     }); 
} 

我希望收到我的接收應用程序的文本1和文本。你能告訴我該怎麼做嗎?或者,如果我想推送一些數據,是否需要以不同的方式發送推送通知?如何推動數據一起推硝化?還有我可以推送多大的數據?

回答

1

如果您的應用收到Toast通知時已經打開,下面的事件處理程序可以得到通知的參數(例如e.Collection[wp:Text1]將返回敬酒的標題):

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

} 

如果您應用程序通過點擊Toast通知打開,您可以在打開應用程序的頁面中實現以下方法。您可以訪問在Toast通知的參數wp:Param的查詢字符串中傳遞的參數。我不確定如何在此方法中獲取Text1和Text2。

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 

    // If we navigated to this page 
    // from the MainPage, the DefaultTitle parameter will be "FromMain". If we navigated here 
    // when the secondary Tile was tapped, the parameter will be "FromTile". 
    textBlockFrom.Text = "Navigated here from " + this.NavigationContext.QueryString["NavigatedFrom"]; 

} 

代碼樣本取自here

+0

如果應用程序未打開,該怎麼辦?如何處理它? @Eran – SD7

相關問題