سؤال

I have this problem with toast notifications, my app shows toast notifications only when it is activated (I.e: when I am using it).

Here is my code for the toast notifications:

    private void ShowToastNotification(string text)
    {
        var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

        xml.GetElementsByTagName("text")[0].AppendChild(xml.CreateTextNode(text));

        ToastNotificationManager.CreateToastNotifier().Show(new ToastNotification(xml));
    }

My application is simply a reminder application, user set time and text and when it is time the app will show toast notification for that text. I simply use a looped timer that checks for reminders each 5 seconds.

    public MainPage()
    {
        this.InitializeComponent();

        DispatcherTimer d = new DispatcherTimer();
        d.Interval = new TimeSpan(0,0,5);
        d.Start();
        d.Tick += delegate
        {
            CHECK();
        };
    }

    private void CHECK()
    {
        foreach (REMINDER_CLASS er in REMINDERS)
        {
            if (DateTime.Now.ToString("MM/dd/yyyy hh:mm:tt") == er.DateTime)
            {
                ShowToastNotification(er.Reminder);

                break;
            }
        }
    }

So when time comes it does not show the toast notification and when I click on the application it shows the notification, it is like it was suspended and when I opened it it resumed.

By the way I have Toast capable set to yes in my appxmanifest and also added BackGround Tasks of type: Timer and System Event inside my Declarations.

The solution could be not allowing the app to sleep or suspend but I don't know how to stop the app from suspending. And the other solution could be BackGround Tasks and I would be thankful if you gave me a good simple resource for background tasks.

هل كانت مفيدة؟

المحلول

I think you're making this harder than it needs to be :) If you used scheduled notifications it would just work and you wouldn't need any background tasks.

If you do want to work with background tasks, there is the Introduction to Background Tasks whitepaper that may be of help.

نصائح أخرى

Based on the details and code you've shared, you should look into the ScheduledToastNotification class. Windows will show scheduled toasts for an app at a specified time, even if the app is not running or is suspended.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top