应用程序运行时接收通知

时间:2013-10-16 13:00:42

标签: c# events windows-phone-8 notifications push-notification

害怕提出之前可能会问过的问题,但我的搜索技巧无法让我找到。好的,所以这里。

我有Windows Phone 8应用程序,当我的应用程序未在前台运行时,我可以收到TileUpdates和Notifications。这是我按照http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202940(v=vs.105).aspx

进行的

在该链接中,我了解到,为了在应用程序运行时收到通知,我应该只为附件案例附加一个事件。我在我的AcquirePushChannel()函数中做了这个,看起来如下:

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


        if (CurrentChannel == null)
        {
            CurrentChannel = new HttpNotificationChannel("MyPushChannel");
            CurrentChannel.Open();
            if (!CurrentChannel.IsShellToastBound)
            {
                CurrentChannel.BindToShellTile();
            }
            CurrentChannel.BindToShellToast();
            CurrentChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(Push_NotificationRecieved);

        }
        if (!CurrentChannel.IsShellTileBound)
        {
            CurrentChannel.BindToShellToast();
            CurrentChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(Push_NotificationRecieved);
        }

            CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
    }

我已经实现了CurrentChannel.ChannelUriUpdated,因为channelUri发生了变化,我执行了一些代码也改变了我在Cloud中的ChannelsTable。 我的Push_NotificationRecieved看起来像:

private static void Push_NotificationRecieved(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.
        MessageBox.Show(message.ToString());
        //Dispatcher.BeginInvoke((message) => MessageBox.Show(message.ToString()));
    }

我看不出为什么没有注册通知。因为在我的云登录中,我收到了收到Toast Notification的信息?

任何想法?此外,我可以从代码或类似的东西中显示吐司,据我所知,这是不可能的吗?

附加

尝试将功能更改为公开但没有帮助解决问题。 任何人都知道为什么事件没有解雇。

2 个答案:

答案 0 :(得分:1)

当然,在我设定奖金后,我开始工作了。所以这是更新的代码。

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


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

    }
    if (!CurrentChannel.IsShellTileBound)
    {
        CurrentChannel.BindToShellToast();
    }

        CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
        CurrentChannel.ShellToastNotificationReceived += CurrentChannel_ShellToastNotificationReceived;
}

好的,原因是你需要在每次启动时设置事件。然后你会得到希望的财产。然后你必须创建自己的代码来获得你想要的东西:)

答案 1 :(得分:1)

您发布的答案几乎是正确的。从以前你有:

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


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

    }
    if (!CurrentChannel.IsShellTileBound)
    {
        CurrentChannel.BindToShellToast();
    }

        CurrentChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(Push_NotificationChannelChanged);
        CurrentChannel.ShellToastNotificationReceived += CurrentChannel_ShellToastNotificationReceived;
}

为此你必须补充:

private static void CurrentChannel_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.
    MessageBox.Show(message.ToString());
}

所以你发送的所有内容都在e.collection中。所以你可以从服务器发送所有类型的参数。