2016-07-27 60 views
0

我正在使用C#和XAML在Windows 10通用中推送通知。一切都很完美。當我收到通知和應用程序運行時我用這個功能做一些事情時,通知的用武之地。如何處理應用程序未激活時的通知?

private async void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e) 
{ 
    e.Cancel = true; 
    //do something in reaction to the notification 
} 

但是,當應用程序在後臺我得到一個徽章的形式Toast通知。當我點擊時,應用程序打開到最後一頁。然而,我想要做的是點擊徽章時,我希望能夠在我的代碼中調用函數。我該怎麼做呢?

回答

1

如果您的應用未在前臺運行,它將被激活,您可以將代碼放入OnActivated事件處理程序中。

根據通知您將使用類型,有在處理小的差異,可能的類型有:

    從使用Windows 10 自適應模板
  • 背景激活從Toast通知
  • 前景激活使用Windows 10自適應模板的烤麪包通知
  • 傳統:前景激活 從使用傳統模板的烤麪包通知。

這是您將使用的前景激活與Windows 10敬酒模板(最常用的一種)的代碼:

protected override void OnActivated(IActivatedEventArgs args) 
{ 
    // TODO: Initialize root frame just like in OnLaunched 

    // Handle toast activation 
    if (args.Kind == ActivationKind.ToastNotification) 
    { 
     var toastArgs = args as ToastNotificationActivatedEventArgs; 

     // your code 
    } 

    // TODO: Handle other types of activation 
} 

您可以按照quickstart for sending and handling activations爲每種類型的樣本。快速啓動還使用NuGet軟件包,讓您的生活更輕鬆。

相關問題