gpt4 book ai didi

c# - 应用程序运行时接收通知

转载 作者:太空宇宙 更新时间:2023-11-03 10:56:13 27 4
gpt4 key购买 nike

害怕问一个以前可能已经问过的问题,但我的搜索技巧却无法找到。好的,开始吧。

我有 Windows Phone 8 应用程序,当我的应用程序未在前台运行时,我可以在其中接收 TileUpdates 和通知。这是我按照 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 发生变化并且我执行一些代码以同时更改云中的 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 通知已收到?

有什么想法吗?此外,据我所知,我可以从代码或类似的东西中显示 toast 吗?

额外

曾尝试将函数更改为公共(public)函数,但没有帮助解决问题。任何人都知道为什么事件没有触发。

最佳答案

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

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 中。所以你可以从服务器发送各种参数。

关于c# - 应用程序运行时接收通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19404123/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com