我正在从我的服务器向应用程序发送通知,但应用程序仅在关闭时收到通知。我知道有一行必须添加,以便您的应用程序在打开时收到通知,并且我的代码中包含该通知。 (见下文)我在2个应用程序中使用相同的代码,结果相同。
public App()
{
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
{
// The channel was already open, so just register for all the events.
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);
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.enter code here
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
pushURI = pushChannel.ChannelUri.ToString();
});
}
}
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
pushURI = e.ChannelUri.ToString();
});
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData));
});
}
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];
}
}
}
任何信息都将受到赞赏。
由于