0
votes

I want to send a toast notification to the action center when my app is in background. So I have set up a Timer, when I start the timer from the app, after 10 sec it produces a toast notification. Now when I try it in the debugger, the toast is produced, even if I press the home button or if I suspend the app using debugger suspend option. But when I deploy the app , it is not producing the toast when I start the timer and click home button. Can anyone suggest a solution for this..?

Adding the Code

    private Timer stateTimer;
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        TimerCallback timerDelegate = new TimerCallback(timer_Tick);
        TimeSpan delayTime = new TimeSpan(0, 0, 10);
        AutoResetEvent autoEvent = new AutoResetEvent(false);
        TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 0);
        Timer notification_timer = new Timer(timerDelegate, autoEvent,   delayTime, intervalTime);
    }

    private void timer_Tick(object state)
    {
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
        toastTextElements[0].AppendChild(toastXml.CreateTextNode("Download has complete"));
        toastTextElements[1].AppendChild(toastXml.CreateTextNode("Download has complete"));

        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        ((XmlElement)toastNode).SetAttribute("duration", "long");

        ((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");
        ToastNotification toast = new ToastNotification(toastXml);
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }
1

1 Answers

1
votes

When you're debugging your app is never suspended until you explicitly choose suspension from the lifecycle events in Visual Studio. So when you press the home button your app actually continues running.

This is not the case when running the app without an attached debugger on an actual device. Once you hit the home button your app gets suspended and won't run until it's resumed.

You would have to use a background task to execute code in background.