在重新启动应用程序时删除计划的通知 [英] Removing scheduled tost notifications on relaunching the app

查看:91
本文介绍了在重新启动应用程序时删除计划的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定时间每天获得通知,无论应用程序是否打开,都应显示通知。甚至用户也可以从win phone 8.1应用程序更改通知时间,以便通知应该在用户选择的时间每天
,甚至可以选择关闭来自应用程序的通知。


        public static ToastNotifier toastNotifier;


        public static void AddScheduleNotification(bool isFlag,DateTime date)

        {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UniquesIds = new List< string>();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(isFlag)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DateTime now = DateTime.Now;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DateTime dueTime = new DateTime(now.Year,now.Month,now.Day,date.Hour,date.Minute,0);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(toastNotifier == null)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; toastNotifier = ToastNotificationManager.CreateToastNotifier();
$


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; for(int i = 0; i< 30; i ++)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var toastXml = GetToast("Toast Header","Toast Body");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml,dueTime);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; scheduledToast.Id = ToastUniqueId + i;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UniquesIds.Add(ToastUniqueId + i);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; toastNotifier.AddToSchedule(scheduledToast);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; dueTime = dueTime.AddDays(1);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; RemoveScheduleNotification();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;抓住
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

$
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; public static void RemoveScheduleNotification()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(toastNotifier!= null)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var scheduled = toastNotifier.GetScheduledToastNotifications();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; for(int i = 0; scheduled!= null&& i< scheduled.Count; i ++)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(scheduled [i]!= null&&!string.IsNullOrEmpty(scheduled [i] .Id)&& UniquesIds.Contains(scheduled [i] .Id))

  ; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; var index = UniquesIds.IndexOf(scheduled [i] .Id);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(scheduled [i] .Id == UniquesIds [index])

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; toastNotifier.RemoveFromSchedule(scheduled [i]);

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;抓住
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //失败了¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }



当用户更改我首先删除预定通知的时间,然后我通过调度安排重新安排通知。但是在将通知添加到计划后我会杀死应用程序,然后重新启动应用程序,然后尝试更改
的时间,但它无法删除以前添加的通知,因为一旦我杀了应用程序
toastnotifier(在我的情况下,它是 ToastNotificationManager.CreateToastNotifier()的静态实例)  实例将为null


如何获取之前添加 toastnotifier 实例,以便我可以删除以前添加的通知。因此,在向计划添加新的通知之前,我应该能够从计划中删除所有以前添加的通知





  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   

解决方案

您好


计划的Toast通知与特定的ToastNotifier实例无关。

因此,即使在关闭并重新打开应用程序之后,通过ToastNotificationManager.CreateToastNotifier()方法创建ToastNotifier的新实例也应该可以删除不需要的预定Toast通知。


I want to get notifictions daily at particular time whether or not the app is open the notification should be displayed. And even user can change the notification time from the win phone 8.1 app so that the notification should come at the user selected time daily and even there is a option to turn off the notification from app.

        public static ToastNotifier toastNotifier;

       public static void AddScheduleNotification(bool isFlag, DateTime date)
        {
            UniquesIds = new List<string>();
            try
            {
                if (isFlag)
                {
                    DateTime now = DateTime.Now;
                    DateTime dueTime = new DateTime(now.Year, now.Month, now.Day, date.Hour, date.Minute, 0);
                    if (toastNotifier == null)
                        toastNotifier = ToastNotificationManager.CreateToastNotifier();

                    for (int i = 0; i < 30; i++)
                    {
                        var toastXml = GetToast("Toast Header", "Toast Body");
                        ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml, dueTime);
                        scheduledToast.Id = ToastUniqueId + i;
                        UniquesIds.Add(ToastUniqueId + i);
                        toastNotifier.AddToSchedule(scheduledToast);
                        dueTime = dueTime.AddDays(1);
                    }
                }
                else
                {
                    RemoveScheduleNotification();
                }
            }
            catch
            {

            }
        }

        public static void RemoveScheduleNotification()
        {
            try
            {
                if (toastNotifier != null)
                {
                    var scheduled = toastNotifier.GetScheduledToastNotifications();

                    for (int i = 0; scheduled != null && i < scheduled.Count; i++)
                    {
                        if (scheduled[i] != null && !string.IsNullOrEmpty(scheduled[i].Id) && UniquesIds.Contains(scheduled[i].Id))
                        {
                            var index = UniquesIds.IndexOf(scheduled[i].Id);
                            if (scheduled[i].Id == UniquesIds[index])
                            {
                                toastNotifier.RemoveFromSchedule(scheduled[i]);
                            }
                        }
                    }
                }
            }
            catch
            {
                //Failed
            }
        }

when user changes the time i am removing the scheduled notifcations first and then i am rescheduling the notification by dding to schedule. but after adding the notifications to schedule i will kill the app and then relaunch the app and then try to change the time but it was unable to remove the previously added notifications because once i kill the app toastnotifier(in my case it is a static instance of ToastNotificationManager.CreateToastNotifier()instance will be null

how to get the previously add toastnotifier instance so that i can remove the previously added notifications.So that before adding new notfications to the schedule i should be able remove all the previously added notifications from schedule


                

解决方案

Hi

The scheduled toast notifications aren't tied to a specific ToastNotifier instance.
So creating a new instance of ToastNotifier through the ToastNotificationManager.CreateToastNotifier() method should be fine to remove the unwanted scheduled toast notification, even after the app being closed and reopened.


这篇关于在重新启动应用程序时删除计划的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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