我正在开发一个wp8应用程序,需求如上:
我的服务器发送了一些我可以在主页上成功处理的原始推送通知。但我有更多的页面,所以我需要我的应用程序继续获取和处理用户在其他页面上的通知。
到目前为止,我已尝试添加与我在主页面中处理通知相同的代码
string channelName = "test";
pushChannel = HttpNotificationChannel.Find(channelName);
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.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
//pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
pushChannel.Open();
// Bind this new channel for toast events.
//pushChannel.BindToShellToast();
System.Threading.Thread.Sleep(3000);
channel = pushChannel.ChannelUri.ToString();
cSettings.device_notify_id = channel;
}
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);
pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(PushChannel_HttpNotificationReceived);
System.Threading.Thread.Sleep(3000);
channel = pushChannel.ChannelUri.ToString();
cSettings.device_notify_id = channel;
// Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
}
和处理程序方法
void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
Dispatcher.BeginInvoke(() =>
{
cSettings.device_notify_id = e.ChannelUri.ToString();
// Display the new URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
});
}
void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
cSettings set = new cSettings();
set.LogEx(new Exception((String.Format("A push notification {0} error occurred. {1} ({2}) {3}",
e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))));
// 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))
);
}
void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
string message;
VibrationDevice vibr = VibrationDevice.GetDefault();
vibr.Vibrate(TimeSpan.FromSeconds(3));
using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body))
{
message = reader.ReadToEnd();
}
cSettings set = new cSettings();
string n_type = "";
string n_header = "";
//var obj = set.parse_stringfromnotify(message, ref n_type, ref n_header);
Dispatcher.BeginInvoke(() => nofication_received_action(message, n_type, ""));
}
private void nofication_received_action(string n_header, string n_type, object data)
{
MessageBoxResult result;
CallSrvData cdata = new CallSrvData();
Exception ex = null;
WP_MemberData m;
WP_MemberRules wpmr;
cSettings set;
MemberRules mr;
Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
Rules c_rules;
Notify.data = data;
Notify.msg_box_text = String.Format("{0}", n_header);
//dose k data sth forma
toast = new Microsoft.Phone.Shell.ShellToast();
toast.Content = "Invitation received";
toast.Title = "Title : ";
//SetProperty(toast, "Sound", new Uri("/data/alert.mp3", UriKind.Relative));
toast.NavigationUri = new Uri("/forms/Notify.xaml?type=0", UriKind.Relative);
toast.Show();
}
当应用程序处于后台并且我成功收到通知并导航到Notify.xaml时,该机制正常工作,但当我返回或按下开始按钮离开Notify.xaml并重新发送通知没有任何反应。我试图在Notify.xaml中添加相同的代码但是当我发送通知时再没有任何反应。与Android相比,您只需在应用程序中注册一次听众,然后您就可以在任何页面中收到通知,即使应用程序已“关闭”,我可以成功完成此类活动,还是可以成功完成此类活动?
为你的贡献做了很多。
答案 0 :(得分:0)
我发现我可以在类中创建所有通知功能,这些功能将在app.cs上初始化,或者需要随时。