无法从Azure移动服务向WP8推送吐司

时间:2013-08-02 02:20:48

标签: c# azure azure-mobile-services windows-phone-8

按照教程

显示Toast通知有问题

Azure移动服务服务器脚本:

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

但是烤面包仍然没有出来(手术用品很好)。

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

编辑: 打开频道时出错:

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: 

1 个答案:

答案 0 :(得分:2)

如果在收到Toast通知时应用程序在前台运行,它将不会在UI中显示Toast;相反,您可以通过订阅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();
    }
}