![](/img/trans.png)
[英]Receive and handle a notification arguments when the Windows (phone) 8.1 app is on foreground
[英]How to handle a notification when app is not active?
我正在使用C#和XAML在Windows 10通用版中執行推送通知。 一切工作正常。 當我收到通知且應用程序正在運行時,當收到通知時,我將使用此功能執行某些操作。
private async void Channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
e.Cancel = true;
//do something in reaction to the notification
}
但是,當應用程序在后台運行時,我會以徽章的形式收到敬酒通知。 當我單擊該應用程序時,它會打開到最后一頁。 但是,我要做的是單擊徽章后,我希望能夠在代碼中調用一個函數。 我該怎么做呢?
如果您的應用程序不在前面運行,它將被激活,您可以將代碼放入OnActivated
事件處理程序中。
根據您將使用的通知類型,處理上會有細微的差別,可能的類型有:
這是您將用於Windows 10 Toast模板(最常用的一個)的前台激活代碼:
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
}
您可以按照此快速入門來發送和處理每種類型的樣本的激活 。 快速入門還使用NuGet包使您的生活更輕松。
您需要在項目中添加Windows運行時組件,以在應用關閉時在后台處理和處理通知。 這是使用Windows運行時組件處理各種觸發器的快速入門 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.