Toast 通知仅在应用程序被激活时弹出 [英] Toast notifications only pop up if the app is being activated

查看:24
本文介绍了Toast 通知仅在应用程序被激活时弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 Toast 通知的问题,我的应用仅在激活时(即:当我使用它时)才会显示 Toast 通知.

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

这是我的 Toast 通知代码:

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));
    }

我的应用程序只是一个提醒应用程序,用户设置时间和文本,到了该应用程序将显示该文本的 toast 通知.我只是使用一个循环计时器,每 5 秒检查一次提醒.

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;
            }
        }
    }

所以当时间到时它不会显示 Toast 通知,当我点击应用程序时它会显示通知,就像它被挂起一样,当我打开它时它又恢复了.

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.

顺便说一下,我在我的 appxmanifest 中将 Toast 功能设置为 yes,并在我的声明中添加了类型为:计时器和系统事件的后台任务.

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.

解决方案可能是不允许应用程序休眠或挂起,但我不知道如何阻止应用程序挂起.另一个解决方案可能是 BackGround Tasks,如果你能给我一个很好的简单的后台任务资源,我将不胜感激.

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.

这篇关于Toast 通知仅在应用程序被激活时弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆