【问题标题】:WNS push notification never fires Activated event when app is suspended当应用程序暂停时,WNS 推送通知永远不会触发 Activated 事件
【发布时间】:2015-03-03 19:47:23
【问题描述】:

我开始头晕目眩:我有一个 windows phone 8.1 通用应用程序。在 App.xaml.cs 中,我实现了以下方法:

public sealed partial class App : Application
{
    public static bool isSuspended = false;

    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
        this.Resuming += this.OnResuming;
    }

    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral();
        App.isSuspended = true;

        // TODO: Save application state and stop any background activity
        deferral.Complete();
    }

    private async void OnResuming(object sender, object e)
    {
        App.isSuspended = false;
    }
}

在我的 MainPageVM(它是一个视图模型)中,我实现了以下方法:

private async void onPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
    switch (args.NotificationType)
    {
        case PushNotificationType.Toast:
            if (App.isSuspended)
            {
                args.ToastNotification.Activated += this.toastNotification_Activated;
            }
            else
            {
                args.Cancel = true;
                this.manageNotification(args.ToastNotification.Content)
            }
            break;
        case PushNotificationType.Raw:
            break;
    }
}

private void toastNotification_Activated(ToastNotification sender, object args)
{
    ToastActivatedEventArgs tArgs = (ToastActivatedEventArgs)args;
    this.manageNotification(tArgs.Arguments);
}

当应用程序被杀死或应用程序处于前台时,它运行良好。当应用程序处于后台时,问题就出现了:当用户点击通知时,toastNotification_Activated 永远不会被提升。

我错过了什么?

【问题讨论】:

  • 当您的应用程序暂停时,它不会执行任何操作,也不会运行任何代码或其他。它只是留在内存中,在某些情况下它可能会被操作系统终止。您的代码 if (App.isSuspended) 不会运行。

标签: c# push-notification windows-phone-8.1 win-universal-app


【解决方案1】:

当您的应用暂停时,这意味着它没有运行 (app lifecycle at MSDN)。在这种情况下,当通知到来时,它不会被您声明的事件拦截,例如MSDN says

注意此过程仅适用于正在运行的应用程序。系统在应用未运行且处理程序未实现时发送的通知会正常传递 - 更新磁贴,显示 toast,并将原始通知传递到后台任务(如果已实现)。

如果你想在后台处理通知,那么你可以考虑声明一个Background Task for it

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2016-08-08
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多