Search code examples
windows-store-appsuwpwin-universal-app

Record Time when user click the close button in Toast notification


Toast notification have a "close button" in the upper right corner.I want to record the time-stamp when user click that "close button" in the UWP application.Is it possible to achieve


Solution

  • Register background task with ToastNotificationHistoryChangedTrigger trigger.

    public class MyBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();
            var details = taskInstance.TriggerDetails as ToastNotificationHistoryChangedTriggerDetail;
            if (details != null)
            {
                if (details.ChangeType == ToastHistoryChangedType.Cleared || details.ChangeType == ToastHistoryChangedType.Removed)
                {
                    // your code - get current time
                }
            }
            deferral.Complete();
        }
    }