c# - 应用程序在后台时不显示通知

标签 c# android xamarin notifications

几天前我开始开发android。我的应用程序需要每天早上和晚上发送通知。如果应用程序在前台,如果它在后台通知只是不显示,它就可以工作。

private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 2)
            {
                GenerateMorningNotification();
            }
            else if (e.ProgressPercentage == 3)
            {
                GenerateEveningNotification();
            }
        }

我使用后台工作程序,在进度更改事件中我使用一种生成通知的方法,例如:

private void GenerisiJutarnjuNotifikaciju()
{
    Notification.Builder builder = new Notification.Builder(this)
        .SetContentTitle("Dobro jutro")
        .SetStyle(new Notification.BigTextStyle().BigText(poruke.RandomPoruka()))
        .SetSmallIcon(Resource.Drawable.Icon);

    Notification notification = builder.Build();

    NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
    const int notificationId = 0;
    notificationManager.Notify(notificationId, notification);        
}

关于我应该更改什么的任何建议?我正在为我女朋友的生日开发这个应用程序,所以我有点赶时间。如果有人可以提供帮助,我将不胜感激。谢谢。

最佳答案

我认为使用 AlarmManager 设置一个 Alarm 来触发一个 BroadcastReceiver 通常是当你希望某事在某个特定时间发生时使用的时间,即使应用程序不在前台。

这里的代码非常详细地说明了这是如何完成的:Send notification once in a week

关于c# - 应用程序在后台时不显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309122/

相关文章:

c# - 如何为特定字符匹配 N 次后的新行?

java - 将目录和文件从 res/raw 文件夹复制到 SD 卡 - android

ios - 如何使用 Xamarin 在 iOS 的 NavigationController 中更改 1 个字符的标题文本颜色

android - 如何将 Android.Resource.Color 转换为 Android.Graphics.Color

ios - 如何在Xamarin iOS项目中无需从设备手动删除App的情况下更新启动画面?

c# - 为什么重用 DataContext 会对性能产生负面影响?

c# - IEnumerable.GroupBy 不分组

c# - F# 命名空间/模块和与 C# 的互操作

android - android listPreferences xml中的条目和条目值有什么区别?

android - appcompat-v7 自定义按钮颜色