Toast notifications only pop up if the app is being activated

落爺英雄遲暮 提交于 2019-12-13 21:21:30

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/16523102/toast-notifications-only-pop-up-if-the-app-is-being-activated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!