【问题标题】:Getting the current page when receiving a toast notification (WP8.1 Silverlight, receiving WNS toast notification)接收toast通知时获取当前页面(WP8.1 Silverlight,接收WNS toast通知)
【发布时间】:2016-02-17 15:26:42
【问题描述】:

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

更新 问题是由于与操作系统线程(调度程序)发生冲突,我无法与元素交互。

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

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

私人无效 OnPushNotificationReceived(PushNotificationChannel 发送者,PushNotificationReceivedEventArgs 参数) { 开关(args.NotificationType) { 案例 PushNotificationType.Badge: this.OnBadgeNotificationReceived(args.BadgeNotification.Content.GetXml()); 休息;

        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 中的当前页面信息。当前的 sn-ps 工作但不在这些功能内:

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

RootFrame.CurrentSource;

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

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

【问题讨论】:

  • 你在哪里定义了 CurrentChannel_PushNotificationReceived 方法?
  • 在项目的 app.xaml.cs 文件中。事件绑定在第一页

标签: silverlight windows-phone-8 windows-phone-8.1 dispatcher


【解决方案1】:

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

注册活动...

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

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

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 线程上执行...您将能够导航页面、获取当前页面等。

【讨论】:

    【解决方案2】:

    好的。尝试这个。在 App.xaml.cs 上创建一个静态属性。

    public static object CurrentPageInfo { get; set; }
    

    并将页面类型或页面名称分配给每个页面上 'OnNavigatedTo' 方法的属性。

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      base.OnNavigatedTo(e);
      var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content;
      App.CurrentPageInfo = currentPage.GetType() is BC_Menu.StartUp.SecondScreen;
    }
    

    这样您就可以通过访问App.CurrentPageInfo 属性来识别接收通知时的页面源类型。希望对您有所帮助!

    【讨论】:

    • 虽然这是一个解决方案,但它并不是一个很好的解决方案。我希望不要引入静态,并且能够直接在函数中查询信息。但也许这是唯一的方法(?):/
    • 您只需要获取页面源类型并且它不适用于推送事件。所以只是给了你替代解决方案。使用静态属性会有什么问题?希望你能得到一些不错的解决方案。很快:)
    • 我可能会开始赏金以澄清是否有其他解决方案。如果没有其他人能找到另一种解决方案,那么嘿,给你更多的积分 :) 欣赏你善意的想法! (也希望)
    • 是的!那会很棒:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多