【发布时间】:2015-03-09 09:20:49
【问题描述】:
我正在开发一个应用程序来接收推送通知。通过使用下面的代码,我获得了频道 uri..现在我的问题是我应该如何将它发送到我正在使用的 web 服务..
public MainPage()
{
HttpNotificationChannel pushChannel;
// The name of our push channel.
string channelName = "ToastSampleChannel";
InitializeComponent();
// Try to find the push channel.
pushChannel = HttpNotificationChannel.Find(channelName);
// If the channel was not found, then create a new connection to the push service.
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.
System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
pushChannel.ChannelUri.ToString()));
}
}
/// <summary>
/// Event handler for when the push channel Uri is updated.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());
MessageBox.Show(String.Format("Channel Uri is {0}",
e.ChannelUri.ToString()));
});
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
// Error handling logic for your particular application would be here.
Dispatcher.BeginInvoke(() =>
MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
);
}
/// <summary>
/// Event handler for when a toast notification arrives while your application is running.
/// The toast will not display if your application is running so you must add this
/// event handler if you want to do something with the toast notification.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void PushChannel_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.
Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString()));
}
在收到 Channel uri 后,我应该如何将它传递给 webserver。我使用了下面的代码但没有工作。
private void RegisterUriWithService()
{
string baseUri = "http://localhost:8000/RegirstatorService/Register?uri={0}";
string theUri = String.Format(baseUri, pushChannel.ChannelUri.ToString());
System.Diagnostics.Debug.WriteLine(theUri);
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, e) =>
{
if (null == e.Error)
Dispatcher.BeginInvoke(() => UpdateStatus("Registration succeeded"));
else
Dispatcher.BeginInvoke(() =>
UpdateStatus("Registration failed: " + e.Error.Message));
};
client.DownloadStringAsync(new Uri(theUri));
}
错误就像
错误:无法将 lambda 表达式转换为类型“System.Delegate”,因为它不是委托类型。 错误:当前上下文中不存在名称“pushChannel”。
Control 没有进入 RegisterUriWithService() 函数。我怎样才能克服这个错误并将通道 uri 发送到 web 服务。 任何人请帮助我.. 非常感谢提前...
【问题讨论】:
标签: c# windows-phone-8 push-notification