locked
[U8.1]Toast Dismissed event RRS feed

  • Question

  • I have a 8.1 Universal App with a  background task that when it finds some new data for the user, it creates a notification, tile, and badge. All that is working. What I'd like to do is take action if the notification is dismissed by the users. I've wired up the dismissed event, but it never gets called.

    ...
                _toast = new ToastNotification(toastXml)
                {
                    ExpirationTime = getExpirationTime(),
     
                };
    
                _toast.Dismissed += Toast_Dismissed;
       
                ToastNotificationManager.CreateToastNotifier().Show(_toast);
        
            }
            
            private void Toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args)
            {
                _toast.Dismissed -= Toast_Dismissed;
               //Do more stuff
            }

    Any ideas?


    Thursday, August 6, 2015 4:15 AM

Answers

  • I would recommend that the background task records the toast it generates into isolated storage (or similar). Then when your app needs to make that decision again you read from that store and ask notification centre for all your toasts. Compare the delta and make your choice.

    http://pauliom.wordpress.com

    • Marked as answer by PKZ Sunday, August 23, 2015 3:38 PM
    Sunday, August 23, 2015 7:12 AM
  • EDIT: The following answer only works on Windows 10, it doesn't work on Windows 8.1. Sorry, didn't notice you had an 8.1 app.

    You need to add a new trigger to your background task, ToastNotificationHistoryChangedTrigger. Then, when anything about your app's toasts changes that wasn't caused by your own app's local executing code (user dismisses a toast, toast expires, new push toast is received), a new background task will be started.

    See this post for a brief explanation on using this.

    Also, the Notifications sample on GitHub contains a working example of this in action. See this page for registering the background task with the trigger, and this page for the background task code that handles the change event.

    The reason the Dismissed event wasn't triggering in your background task is because the Dismissed event is bound to the current toast object that your background task created. Most likely your background task has already terminated by the time the user dismisses the toast, and thus the toast object and the event doesn't exist since your code is no longer running. That's why you create a trigger for ToastNotificationHistoryChangedTrigger which will cause a new background task to start up when the user dismisses your toast.






    • Proposed as answer by andrewbares7 Thursday, September 3, 2015 5:50 PM
    • Marked as answer by PKZ Thursday, September 3, 2015 6:48 PM
    • Edited by andrewbares7 Thursday, September 3, 2015 7:54 PM
    Thursday, September 3, 2015 5:49 PM

All replies

  • Hi PKZ,

     _toast.Dismissed -= Toast_Dismissed;


    I see that you are detaching the Toast_Dismissed event handler when that event is trigged by the user. I'm not sure why you're doing that? This could be one of the reasons why it isn't working

    Can you try this in the debug mode by putting up a break point inside the Toast_Dismissed event handler and see if you're hitting the breakpoint when you dismiss the notification?

    Thursday, August 6, 2015 6:27 AM
  • I've tried debugging it and the event never gets hit. I un-wire the event in the handler because each time the background process finds data and send the toast, it wires it up again.

    FWIW, I'm developing this on a Windows 10 box.

    Thursday, August 6, 2015 2:26 PM
  • Hi PKZ,

    Since I do not have all your code, so I have created the following example and run it on the Windows 10, it works fine, the Toast Dismissed event will fire either by expiring or being explicitly dismissed by the user. So please try to test my code in your side to see if it works.

            ToastNotification _toast;
            private void NotificationButton_Click(object sender, RoutedEventArgs e)
            {
                var notificationXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
    
                var toeastElement = notificationXml.GetElementsByTagName("text");
    
                toeastElement[0].AppendChild(notificationXml.CreateTextNode("This is Notification Message"));
            
                _toast = new ToastNotification(notificationXml)
                {
                    ExpirationTime = DateTimeOffset.Now.AddSeconds(5)
    
                };
    
                ToastNotificationManager.CreateToastNotifier().Show(_toast);
                _toast.Dismissed += _toast_Dismissed;
    
            }
    
            async void _toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args)
            {
                _toast.Dismissed -= _toast_Dismissed;
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NotificationButton.Content = "The Toast is missing"; });
            }
    Besides, it will be better if you can try to post a simple reproduced project in here.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, August 14, 2015 9:18 AM
  • Thanks for the reply, but the example is not duplicating the way my app is setup. As I stated previously, the toast is created in a background task and the  dismissed event is wired up there too. So move your toast creation and event wire up code from .xaml.cs to a background task and try it there.

     

    I appreciate your efforts in helping me to get this to work.

    Saturday, August 15, 2015 3:15 AM
  • Thanks for the reply, but the example is not duplicating the way my app is setup. As I stated previously, the toast is created in a background task and the  dismissed event is wired up there too. So move your toast creation and event wire up code from .xaml.cs to a background task and try it there.

     

    I appreciate your efforts in helping me to get this to work.

    Friday, August 21, 2015 6:48 PM
  • I'm 99% sure your background task will have been closed by the time the user even see's the toast. You can't rely on requiring any kind of user input in a background task. What is the scenario that you are trying to do?

    http://pauliom.wordpress.com

    Saturday, August 22, 2015 6:28 AM
  • I'm guessing that is the problem too.

    What I'd like to do if the notification is dismissed by the user is to update some stored data so the next time I check to see if they missed something, I don't notify them (they dismissed the notification about that data).

    If this were email, I'd mark the email as "Read".

    How should it be handled?

    Pete

    Saturday, August 22, 2015 9:55 PM
  • I would recommend that the background task records the toast it generates into isolated storage (or similar). Then when your app needs to make that decision again you read from that store and ask notification centre for all your toasts. Compare the delta and make your choice.

    http://pauliom.wordpress.com

    • Marked as answer by PKZ Sunday, August 23, 2015 3:38 PM
    Sunday, August 23, 2015 7:12 AM
  • Thanks... I'll give that approach a try.
    Sunday, August 23, 2015 3:39 PM
  • EDIT: The following answer only works on Windows 10, it doesn't work on Windows 8.1. Sorry, didn't notice you had an 8.1 app.

    You need to add a new trigger to your background task, ToastNotificationHistoryChangedTrigger. Then, when anything about your app's toasts changes that wasn't caused by your own app's local executing code (user dismisses a toast, toast expires, new push toast is received), a new background task will be started.

    See this post for a brief explanation on using this.

    Also, the Notifications sample on GitHub contains a working example of this in action. See this page for registering the background task with the trigger, and this page for the background task code that handles the change event.

    The reason the Dismissed event wasn't triggering in your background task is because the Dismissed event is bound to the current toast object that your background task created. Most likely your background task has already terminated by the time the user dismisses the toast, and thus the toast object and the event doesn't exist since your code is no longer running. That's why you create a trigger for ToastNotificationHistoryChangedTrigger which will cause a new background task to start up when the user dismisses your toast.






    • Proposed as answer by andrewbares7 Thursday, September 3, 2015 5:50 PM
    • Marked as answer by PKZ Thursday, September 3, 2015 6:48 PM
    • Edited by andrewbares7 Thursday, September 3, 2015 7:54 PM
    Thursday, September 3, 2015 5:49 PM
  • Thanks for the full explanation. The first 3 links are not working however.
    Thursday, September 3, 2015 6:55 PM
  • Sorry, Edge doesn't work well with editing links on this form. The links are fixed now!
    Thursday, September 3, 2015 7:48 PM
  • Also, I just realized you have an 8.1 app. My answer only works for 10 apps (the ToastNotificationHistoryChangedTrigger is a new feature for Windows 10 apps).

    It's basically impossible to achieve your scenario on 8.1. That's why we added that new feature to 10.

    Thursday, September 3, 2015 7:59 PM
  • Thanks for your input.
    Thursday, September 3, 2015 9:42 PM