Ios 处理本地通知的最佳方法

Ios 处理本地通知的最佳方法,ios,notifications,uilocalnotification,Ios,Notifications,Uilocalnotification,我有以下问题: 我有两个日历,都需要创建本地通知(每个事件前5分钟触发)。在设置中,用户可以打开或关闭任一日历的通知。如果用户最初使用两个日历的通知,现在只想使用一个日历的通知,我如何才能只删除一个日历中的通知 我想我有3个选择: 正在运行[[UIApplication sharedApplication]cancelAllLocalNotifications]然后将其他日历中的所有日历都添加回去(这可能比听起来更难) 将已创建通知的数组存储在类似用户默认值的格式中,然后通过数组循环调用:[[U

我有以下问题:

我有两个日历,都需要创建本地通知(每个事件前5分钟触发)。在设置中,用户可以打开或关闭任一日历的通知。如果用户最初使用两个日历的通知,现在只想使用一个日历的通知,我如何才能只删除一个日历中的通知

我想我有3个选择:

  • 正在运行
    [[UIApplication sharedApplication]cancelAllLocalNotifications]
    然后将其他日历中的所有日历都添加回去(这可能比听起来更难)
  • 将已创建通知的数组存储在类似用户默认值的格式中,然后通过数组循环调用:
    [[UIApplication sharedApplication]cancelLocalNotification:notification]
  • 子类化
    UILocalNotification
    ,并添加一些字段,允许我对通知进行排序。然后,也许我可以调用
    [[UIApplication sharedApplication]ScheduledLocalNotification]
    并循环检查该字段并删除必要的字段

  • 有没有一个标准的方法可以做到这一点?我认为第三种可能是最简单的,但我不确定它是否有效

    UILocalNotification
    有一个标准的
    userInfo
    属性,只要键是有效的属性列表类型,该属性就是任意值的
    NSDictionary
    。如果子类
    UILocalNotification
    ,则必须使用该字典作为要持久化的其他属性或字段的后备存储。为了能够使用您的子类,您需要一个初始化方法,将属性从基类复制到您的子类

    #define kNoficationCalendarName NSStringFromSelector(@selector(calendarName))
    
    @interface XXLocalNotification : UILocalNotification
    
    @property (nonatomic, strong) NSString * calendarName;
    
    - (instancetype)initWithLocalNotification:(UILocalNotification *)notification;
    
    @end
    
    @implementation XXLocalNotification
    
    - (instancetype)initWithLocalNotification:(UILocalNotification *)notification
    {
        self = [super init];
        if (self)
        {
            //Copy properties
            self.alertAction = notification.alertAction;
            self.alertBody = notification.alertBody;
            self.alertLaunchImage = notification.alertLaunchImage;
            self.applicationIconBadgeNumber = notification.applicationIconBadgeNumber;
            self.fireDate = notification.fireDate;
            self.hasAction = notification.hasAction;
            self.repeatCalendar = notification.repeatCalendar;
            self.repeatInterval = notification.repeatInterval;
            self.soundName = notification.soundName;
            self.timeZone = notification.timeZone;
            self.userInfo = notification.userInfo;
        }
        return self;
    }
    -(void)setCalendarName:(NSString *)calendarName
    {
        NSMutableDictionary * userInfo = [[self userInfo] mutableCopy];
        [userInfo setValue:calendarName
                    forKey:kNoficationCalendarName];
    }
    - (NSString *)calendarName
    {
        return [[self userInfo] valueForKey:kNoficationCalendarName];
    }
    
    @end