接收Toast通知时获取当前页面(WP8.1 Silverlight,接收WNS Toast通知) [英] Getting the current page when receiving a toast notification (WP8.1 Silverlight, receiving WNS toast notification)

查看:111
本文介绍了接收Toast通知时获取当前页面(WP8.1 Silverlight,接收WNS Toast通知)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件,该事件在应用程序启动时触发,并且收到通知 CurrentChannel_PushNotificationReceived 。在此功能中,我想找出当前显示的页面,以了解通知是否应更新页面上的内容。因此,问题是双重的,即如何知道当前显示的页面并与Toast通知进行交互。

I have an event that fires when the app is live and I receive an notification CurrentChannel_PushNotificationReceived. In this function I want to find out which page is currently displayed to know if the notification should update content on the page. The question is therefore twofold, how to know which page is currently displayed and interact with the toast notification.

更新

因此,使用以下代码,它使我可以访问消息的内容。但是我仍然无法获取current_page

Therefore using the below code it allows me to access the content of the message. But I am still not able to get the info of the current_page

  _channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
_channel.PushNotificationReceived += OnPushNotificationReceived;

私有无效OnPushNotificationReceived(PushNotificationChannel sender,PushNotificationReceivedEventArgs args)
{
switch( args.NotificationType)
{
case PushNotificationType.Badge:
this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml());
break;

private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) { switch (args.NotificationType) { case PushNotificationType.Badge: this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml()); break;

        case PushNotificationType.Tile:
            this.OnTileNotificationReceived(args.TileNotification.Content.GetXml());
            break;

        case PushNotificationType.Toast:
            this.OnToastNotificationReceived(args.ToastNotification.Content.GetXml());
            break;

        case PushNotificationType.Raw:
            this.OnRawNotificationReceived(args.RawNotification.Content);
            break;
    }

    args.Cancel = true;
}

private void OnBadgeNotificationReceived(string notificationContent)
{
    // Code when a badge notification is received when app is running
}

private void OnTileNotificationReceived(string notificationContent)
{
    // Code when a tile notification is received when app is running
}

private void OnToastNotificationReceived(string notificationContent)
{
    // Code when a toast notification is received when app is running

    // Show a toast notification programatically

    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(notificationContent);
    var toastNotification = new ToastNotification(xmlDocument);

    //toastNotification.SuppressPopup = true;
    ToastNotificationManager.CreateToastNotifier().Show(toastNotification);
}

private void OnRawNotificationReceived(string notificationContent)
{
    // Code when a raw notification is received when app is running
}

问题

如何如何在不同的 onXXXXNotificationReceived 中访问当前页面信息。当前代码段有效,但不适用于以下功能:

How do I access the current page information in the different onXXXXNotificationReceived. The current snippets work but not within these functions:

var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
        var tempBool = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;

RootFrame.CurrentSource;

我的猜测是因为UI线程。那么如何使用调度程序来获取信息?我已经与调度程序一起尝试了一些解决方案,但是我无法等待这些信息,因此它不适用。

My guess is it is because of the UI-thread. So how can I use the dispatcher to get the information? I have tried some solutions with the dispatcher, but I cannot await the information, and therefore it is not applicable.

System.Windows.Threading.DispatcherOperation op = App.RootFrame.Dispatcher.BeginInvoke(new Func<Uri>(() =>  
            {
                return RootFrame.CurrentSource;
            })
        );
        await op; //Not awaitable.


推荐答案

没有理由等待UI线程的调度程序。只需从UI线程内调度到UI线程,然后执行其余逻辑,例如显示吐司或导航到页面...

There's no reason to await the dispatcher to the UI thread. Simply dispatch to the UI thread and then perform the rest of your logic, like displaying the toast or navigating to a page, from within the UI thread...

注册事件...

var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
channel.PushNotificationReceived += Channel_PushNotificationReceived;

在事件处理程序上,取消显示通知,然后分派到UI线程...

On the event handler, cancel displaying the notification and then dispatch to UI thread...

private void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
    // Make sure you cancel displaying the toast on this thread (not on UI thread)
    // since cancellation needs to be set before this thread/method returns
    args.Cancel = true;

    // Then dispatch to the UI thread
    App.RootFrame.Dispatcher.BeginInvoke(delegate
    {
        var currPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;

        switch (args.NotificationType)
        {
            case PushNotificationType.Toast:
                // TODO
                break;
        }
    });
}

在调度员的委托中执行所有代码。您所有的代码都将在UI线程上执行...您将能够浏览页面,获取当前页面等。

Do all of your code inside the dispatcher's delegate. All your code will be executing on the UI thread... you'll be able to navigate pages, obtain current page, etc.

这篇关于接收Toast通知时获取当前页面(WP8.1 Silverlight,接收WNS Toast通知)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆