c# - 当应用程序不在使用 Xamarin Crossplatform 的设备的最近历史记录中时如何显示本地通知

标签 c# xaml xamarin.forms

我想在应用关闭时显示本地通知,而且应用不在设备的最近历史记录中。我该怎么做。

请帮帮我..

这是我的代码 MainPage.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        var startTimeSpan = TimeSpan.Zero;
        var periodTimeSpan = TimeSpan.FromMinutes(5);

        Device.StartTimer(TimeSpan.FromSeconds(5), () =>
        {
            GetData();

            return true; 
        });
    }

    public void GetData()
    {
        IAlarmReceiver _local = DependencyService.Get<IAlarmReceiver>();
        _local.LocalNotifications("Xamarin", "Hello world");
    }

    private void btn_Click_Clicked(object sender, EventArgs e)
    {
        GetData();
    }
}

这里我每 5 秒调用一次通知方法。

这是我的 AlarmReciever.CS

    public void GetNotification(string Title, string Text)
    {
        Intent _intent = new Intent(Application.Context, typeof(MainActivity));
        const int _pendingIntentId = 0;
        PendingIntent _pintent = PendingIntent.GetActivity(Application.Context, _pendingIntentId, _intent, PendingIntentFlags.CancelCurrent);

        Notification.Builder builder = new Notification.Builder(Application.Context)
       .SetContentIntent(_pintent)
       .SetContentTitle(Title)
       .SetContentText(Text)
       .SetDefaults(NotificationDefaults.Sound)
       .SetAutoCancel(true)
       .SetSmallIcon(Resource.Drawable.icon);

        Notification _notification = builder.Build();
        NotificationManager _notificationmanager = Android.App.Application.Context.GetSystemService(Context.NotificationService)
            as NotificationManager;

        _notificationmanager.Notify(0, _notification);

最佳答案

您可以使用 Xamarin 的 ACR 通知插件,这将在应用关闭时显示本地通知。

示例 await CrossNotifications.Current.Send("Happy Birthday", "I sent this a long time ago", when = TimeSpan.FromDays(50));

关于c# - 当应用程序不在使用 Xamarin Crossplatform 的设备的最近历史记录中时如何显示本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788344/

相关文章:

c# - 正则表达式 ~ 将 UNC 转换为 URL

c# - 在 Winform 中托管 RemoteAPP session

xaml - Xamarin Forms 药丸按钮

xaml - Xamarin 标签单击更改复选框状态仅 Xaml

c# - 通过 ComboBox 更改 RichTextBox Selection 的字体大小无法正常工作

http - 将 HttpResponseMessage 转换为对象

c# - ProtoBuf WCF 行为的配置错误

c# - 从窗口中提取键盘布局

c# - 切换到 IPV6 时 HttpClient 抛出异常 - 被 iOS 商店拒绝

android - 带有 Mvvmcross 的 Xamarin 表单选项卡式页面