应用程序打开时无法显示PUSH通知

14

我的应用程序在关闭时能够接收到推送通知,但在运行时却没有任何反应。这段代码与之前在WindowsPhone8上使用的相同,并且没有遇到任何问题,但是新的应用程序正在运行WindowsPhone8.1设备。

我在创建原始应用程序时使用了Push 教程。我已经添加了一行代码,以便在应用程序打开时接收通知。

如果8.1更新对推送通知进行了更改,那么希望能得到通知。也感谢其他任何建议。

HttpNotificationChannel pushChannel;
string channelName = "PushChannel";
pushChannel = HttpNotificationChannel.Find(channelName);
//Push Notifications
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);

    // Register for this notification only if you need to receive 
    // the notifications while your application is running.
    pushChannel.ShellToastNotificationReceived += 
      new EventHandler<NotificationEventArgs>(
         PushChannel_ShellToastNotificationReceived);

    pushChannel.Open();

    // Bind this new channel for toast events.
    pushChannel.BindToShellToast();

}
else...



void PushChannel_ShellToastNotificationReceived(object sender, 
                                                         NotificationEventArgs e)

{
    string relativeUri = string.Empty;

    // Parse out the information that was part of the message.
    foreach (string key in e.Collection.Keys)

    {
        if (string.Compare(
        key,
        "wp:Param",
        System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.CompareOptions.IgnoreCase) == 0)

        {
            relativeUri = e.Collection[key];
        }


    }
}

已添加代码。否则只需将事件处理程序添加到已存在的PushChannel中。 - Seige
弹出通知根本没有显示,就好像推送监听器没有在监听一样。我不确定这是否完全正确,但我认为当单击弹出通知时,ShellToastNotificationRecieved会被触发。但是我没有收到任何弹出通知。 - Seige
2
Toast 不会在应用程序在前台时显示。如果需要,应用程序应该显示自己的用户界面(你的代码片段没有展示任何东西)。这就是 ShellToastNotificationReceived 事件的作用:当 Toast 通知到达而不是显示 Toast 时触发它。请确认 ShellToastNotificationReceived 在预期 Toast 弹出时是否被调用,这是应该发生的。请确认在调试器中已经为其注册并是否收到了它(或未收到)。详见 http://msdn.microsoft.com/zh-cn/library/windows/apps/hh202940(v=vs.105).aspx - Rob Caplan - MSFT
在8.1更新之前,当一个打开的应用程序接收到PUSH时,弹出窗口仍然会显示。我刚刚进行了一些测试,确实,“PushChannel_ShellToastNotificationReceived”事件仍在被触发,但是弹出窗口没有显示。我想这意味着我需要以不同的方式处理它。如果你想把它变成一个答案,我可以授予它奖励。 - Seige
我刚才被编辑了、被踩了并且失去了声望,是因为水平滚动条吗? - Seige
显示剩余2条评论
1个回答

2

Rob Caplan:

Toast提示在应用程序处于前台时不会显示。如果需要,应用程序会展示自己的用户界面(你的代码片段没有展示任何内容)。这就是ShellToastNotificationReceived事件的作用:当Toast通知到达而不是Toast出现时,它就会触发。你能确认在你期望Toast时ShellToastNotificationReceived没有被触发吗?它应该被触发的。你能确认在调试器中是否注册并接收到了它或者未接收到吗?参见msdn.microsoft.com/en-us/library/windows/apps/…

Me:

在8.1更新之前,如果打开的应用程序接收到PUSH,Toast仍然会显示。我刚刚进行了一些测试,果然“PushChannel_ShellToastNotificationReceived”事件仍在触发,但Toast没有显示。我想这意味着我需要使用不同的方式来处理它。如果你想将其转化为答案,我可以授予它奖励。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接