2013-08-02 44 views
1

我得到了一些問題,通過下面的教程無法從Azure的推舉杯WP8移動服務

這裏在Azure移動服務服務器腳本顯示Toast通知:

function insert(item, user, request) { 
request.execute({ 
    success: function() { 
     // Write to the response and then send the notification in the background 
     request.respond(); 
     push.mpns.sendToast(item.channel, { 
      text1:"Sent from cloud!" 
     }, { 
      success: function (pushResponse) { 
       console.log("Sent push:", pushResponse); 
      } 
     }); 
    } 
}); 

這是編碼我放在App.xaml.cs中:

//push notification 
    public static HttpNotificationChannel CurrentChannel { get; private set; } 


    private void AcquirePushChannel() 
    { 
     CurrentChannel = HttpNotificationChannel.Find("MyPushChannel"); 


     if (CurrentChannel == null) 
     { 
      CurrentChannel = new HttpNotificationChannel("MyPushChannel"); 
      CurrentChannel.Open(); 
      //CurrentChannel.BindToShellTile(); 
      CurrentChannel.BindToShellToast(); 
     } 
    } 

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

但是,吐司仍然沒有出來(fliptile工作正常)。

任何修改需要使吐司工作?

編輯: 錯誤打開時信道:

System.InvalidOperationException was unhandled by user code 
    HResult=-2146233079 
    Message=Open failed because the channel was already open. You can find an open channel by calling the Find method. 
    Source=Microsoft.Phone 
    StackTrace: 
     at Microsoft.Phone.Notification.SafeNativeMethods.ThrowExceptionFromHResult(Int32 hr, Exception defaultException, NotificationType type) 
     at Microsoft.Phone.Notification.HttpNotificationChannel.Open() 
     at UtemFtmkDB.App.AcquirePushChannel() 
     at UtemFtmkDB.App.Application_Launching(Object sender, LaunchingEventArgs e) 
     at Microsoft.Phone.Shell.PhoneApplicationService.FireLaunching() 
     at Microsoft.Phone.TaskModel.Interop.ITask.Launching.Invoke() 
     at Microsoft.Phone.TaskModel.Interop.Task.FireOnLaunching() 
    InnerException: 

回答

2

如果應用程序上接收土司通知時前臺運行,它不會顯示在用戶界面中的麪包;相反,您可以通過訂閱ShellToastNotificationReceived event來獲得。如果你這樣做,你會收到關於事件處理程序的通知。爲了防止InvalidOperationException同時呼籲Open,您可以使用下面的代碼:在這個問題更新後

編輯

private void AcquirePushChannel() 
{ 
    CurrentChannel = HttpNotificationChannel.Find("MyPushChannel"); 

    if (CurrentChannel == null) 
    { 
     CurrentChannel = new HttpNotificationChannel("MyPushChannel"); 
    } 

    if (CurrentChannel.ConnectionStatus == ChannelConnectionStatus.Disconnected) 
    { 
     CurrentChannel.Open(); 
    } 

    if (!CurrentChannel.IsShellToastBound) 
    { 
     CurrentChannel.BindToShellToast(); 
    } 
} 
+0

CurrentChannel.Open(); 未知模塊中發生類型'System.InvalidOperationException'的異常。但沒有在用戶代碼中處理 這是什麼錯誤,昨天沒有這個 –

+0

有沒有關於異常的更多信息?消息,堆棧跟蹤等? – carlosfigueira

+0

嗨@carlosfigueira,我更新了我的問題中的錯誤 –