【发布时间】: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