Custom Master Data Subscribers for Transformable UILocalNotification

I have a transformable attribute on one of my objects called a reminder. This is a UILocalNotification.

Now, since I want to schedule it when adding and cancel it when deleting, I would like to redefine accessors to handle scheduling and cancellation there.

What would it look like?

Thank!

+3
source share
2 answers

Are you really saving UILocalNotification or are you using it as a transient property?

, UILocalNotification userInfo . / . :

notificationID userInfo notificationID Core Data . , int NSString ( ).

UILocalNotification, Entity, :

- (void)createNotification
{
    static NSUInteger kDeadlineWarningPeriod = 3600;
    UILocalNotification *notification = [[UILocalNotification alloc] init];


    self.notificationID = @"some generated ID";

    [notification.userInfo setValue:self.notificationID forKey:@"notificationID"];

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    [notification release];
}

- (void)cancelNotification
{
    // We search for the notification.
    // The entity ID will be stored in the notification user info.

    [[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    UILocalNotification *notification = (UILocalNotification *)obj;
    NSDictionary *userInfo = notification.userInfo;

    NSString *notificationID = [userInfo valueForKey:@"notificationID"];

    if ([notificationID isEqualToString:self.notificationID])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
        *stop = YES;
                    self.notificationID = nil;
    }
     }];
}

, , .

, .

UPDATE

, , Entity ( , BOOL), :

// .h

@property (nonatomic, assign) BOOL reminder;

// .m

- (void)setReminder:(BOOL)reminder {

   [self willAccessValueForKey@"reminder"];
   BOOL hasReminder = [[self primitiveValueForKey:@"reminder"] booleanValue];
   [self didAccessValueForKey:@"reminder"];

   if (hasReminder && !reminder) {

        [self cancelNotification];
   }
   else if (!hasReminder && reminder) {

        [self createNotification];
   }

   if (reminder != hasReminder)
   {
        [self willChangeValueForKey:@"reminder"];
        [self setPrimitiveValue:[NSNumber numberWithBool:reminder] forKey@"reminder"];
        [self didChangeValueForKey:@"reminder"];
   }
}

"" , , notificationID nil . .

, - .

, , 64 , . , , .

+9

, objectId NSManagedObject Persistent.

objectId :

[[self.objectID URIRepresentation] absoluteString] 

[[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:[NSURL URLWithString:[localNotif.userInfo objectForKey: kYourReminderNotificationKey]

:

- (void)createNotification
{        

    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {

        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = self.dateDue;
        notif.timeZone = [NSTimeZone defaultTimeZone];


       notif.alertBody = @"Alert body";        

        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;


        NSDictionary *userDict = [NSDictionary dictionaryWithObject:[[self.objectID URIRepresentation] absoluteString] forKey:kRemindMeNotificationDataKey];


        notif.userInfo = userDict;

        [[UIApplication sharedApplication] scheduleLocalNotification:notif];

    }


}

- (void)cancelNotification
{

    [[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        UILocalNotification *notification = (UILocalNotification *)obj;
        NSDictionary *userInfo = notification.userInfo;

        NSString *notificationID = [userInfo valueForKey:kRemindMeNotificationDataKey];

        if ([notificationID isEqualToString:[[self.objectID URIRepresentation] absoluteString]])
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            *stop = YES;
        }
    }];
}
0

Source: https://habr.com/ru/post/1769940/


All Articles