警报管理器本地通知 - 如何取消特定通知? [英] alarm manager local notification - how to cancel specific notification?

查看:23
本文介绍了警报管理器本地通知 - 如何取消特定通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过警报管理器安排我的客户在我设置的时间收到通知.现在一切正常,我收到本地通知,但我无法取消该特定通知,它每分钟都会出现.

i am scheduling my customer through alarm manager to get notification on time that i set. Now everything is working fine i get local notification, but i am not able to cancel that specific notification its keep coming after every minute.

这是我的视图模型代码 PCL取消:

here is my viewmodel code PCL for cancel :

void Switch_Toggled()
        {

            if (NotificationONOFF == false)
            {

                MessageText = string.Empty;
                SelectedTime = DateTime.Now.TimeOfDay;
                SelectedDate = DateTime.Today;
                DependencyService.Get<ILocalNotificationService>().Cancel(Convert.ToInt32(Settings.Customerid));
            }
        }

保存闹钟

DependencyService.Get<ILocalNotificationService>().LocalNotification("Local Notification", MessageText, Convert.ToInt32(Settings.Customerid) , selectedDateTime);

xamarin.android 中的代码对于canel:

code in xamarin.android for canel:

public void Cancel(int id)
        {

            var intent = CreateIntent(id);
            var pendingIntent = PendingIntent.GetBroadcast(Application.Context, Convert.ToInt32(_randomNumber), intent, PendingIntentFlags.Immutable);
            var alarmManager = GetAlarmManager();
            alarmManager.Cancel(pendingIntent);
            var notificationManager = NotificationManagerCompat.From(Application.Context);
            notificationManager.CancelAll();
            notificationManager.Cancel(id);
        }

我正在发送客户 ID 以取消,但它不起作用.

i am sending customer id to cancel but its not working.

推荐答案

根据您的描述,您有 xamarin.forms 示例,使用警报管理器发送具有重复间隔的本地通知,如下示例:

According to your description, you have xamarin.forms sample, sending Local Notification With A Repeat Interval using Alarm Manager, like this sample:

https://www.c-sharpcorner.com/article/how-to-send-local-notification-with-repeat-interval-in-xamarin-forms/

有两种解决方案,但我不知道你是什么.

There are two solution, but I don't know what you are.

解决方案 1:

当您运行项目并设置一个闹钟管理器以重复间隔发送本地通知时,此应用程序现在处于后台.

When you run project and set one alarm Manager to send local notification with a repeat interval, this app is background now.

时间到了,你进入这个后台应用,你发现开关没有被选中,但是闹钟仍然存在.所以你想在你进入这个应用程序时取消这个闹钟,对吗?

When time is up, you go into this background app, you find switch is unselect, but alarm still exist. So you want to cancel this alarm when you go into this app, am I right?

如果是,我建议您可以在 LocalNotificationPageViewModel 构造函数中调用 Switch_Toggled() 事件.

If yes, I suggest you can call Switch_Toggled() event in LocalNotificationPageViewModel constructor.

public LocalNotificationPageViewModel(){
        SaveCommand = new Command(() => SaveLocalNotification());
        Switch_Toggled();
    }

解决方案 2:

如果您运行项目并设置一个闹钟管理器以重复间隔发送本地通知,并且此应用程序仍然处于活动状态,现在时间到了,您可以取消选择开关以取消此闹钟,它工作正常.

If you run project and set one alarm Manager to send local notification with a repeat interval, and this app still active, now time is up, you can unselect switch to cancel this alarm, it works fine.

这篇关于警报管理器本地通知 - 如何取消特定通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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