将 Channel Uri 发送到云服务
Posted
技术标签:
【中文标题】将 Channel Uri 发送到云服务【英文标题】:Sending Channel Uri to cloud service 【发布时间】: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 服务。 任何人请帮助我.. 非常感谢提前...
【问题讨论】:
【参考方案1】:RegisterUriWithService() 不会从代码中的任何位置调用。
像这样重写你的方法
private void RegisterUriWithService(string pushChannelUri) string baseUri = "http://localhost:8000/RegirstatorService/Register?uri=0"; string theUri = String.Format(baseUri, pushChannelUri); // pushChannelUri is passed as parameter to this method 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));
当您的推送频道打开并更新频道 URI 时,将触发 PushChannel_ChannelUriUpdated 事件。
将推送通道 URI 存储在局部变量 pushChannelUri 中并从那里调用方法 RegisterUriWithService(pushChannelUri)。
如果推送通道已经存在且带有 URI,则控制转到 else 块
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())); pushChannelUri = pushChannel.ChannelUri.ToString();
调用方法从此块中注册UriWithService(pushChannelUri)。
【讨论】:
是的,我已经完成了相同的过程..从 else 块调用了 RegisterUriWithService。它对我来说很好......谢谢......以上是关于将 Channel Uri 发送到云服务的主要内容,如果未能解决你的问题,请参考以下文章