diff --git a/.travis.yml b/.travis.yml index 89eee85..e615488 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,5 +5,7 @@ cache: before_install: - rvm get stable - rvm install ruby-2.0.0-p247 + - gem install cocoapods + - pod install script: - bundle exec rake default diff --git a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h index 9f546c4..1c64f44 100644 --- a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h +++ b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h @@ -14,11 +14,15 @@ extern NSString *const JRNApplicationDidReceiveLocalNotification; typedef void (^JRNLocalNotificationHandler)(NSString *key, NSDictionary *userInfo); @interface JRNLocalNotificationCenter : NSObject + +@property (nonatomic, readonly) NSDictionary *scheduledLocalNotificationsByKey; @property (nonatomic, copy) JRNLocalNotificationHandler localNotificationHandler; + (instancetype)defaultCenter; -- (NSArray *)localNotifications; +//Checking +- (BOOL)isLocalNotificationScheduledForKey:(NSString *)key; +- (NSDate *)fireDateForExistingScheduledNotificationForKey:(NSString *)key; //Handling - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo; @@ -77,4 +81,14 @@ typedef void (^JRNLocalNotificationHandler)(NSString *key, NSDictionary *userInf badgeCount:(NSUInteger)badgeCount repeatInterval:(NSCalendarUnit)repeatInterval; +- (UILocalNotification *)postNotificationOn:(NSDate *)fireDate + forKey:(NSString *)key + alertBody:(NSString *)alertBody + alertAction:(NSString *)alertAction + soundName:(NSString *)soundName + launchImage:(NSString *)launchImage + userInfo:(NSDictionary *)userInfo + badgeCount:(NSUInteger)badgeCount + repeatInterval:(NSCalendarUnit)repeatInterval; + @end diff --git a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m index 2b32a8f..362d0da 100644 --- a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m +++ b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m @@ -12,42 +12,62 @@ NSString *const JRNApplicationDidReceiveLocalNotification = @"JRNApplicationDidReceiveLocalNotification"; @interface JRNLocalNotificationCenter() -@property (nonatomic) NSMutableDictionary *localPushDictionary; +@property (nonatomic) NSMutableDictionary *mutableScheduledLocalNotificationsByKey; @property (nonatomic) BOOL checkRemoteNotificationAvailability; @end -static JRNLocalNotificationCenter *defaultCenter; - @implementation JRNLocalNotificationCenter -+ (instancetype)defaultCenter -{ ++ (instancetype)defaultCenter { + static JRNLocalNotificationCenter *defaultCenter; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - defaultCenter = [JRNLocalNotificationCenter new]; - defaultCenter.localPushDictionary = [NSMutableDictionary new]; - [defaultCenter loadScheduledLocalPushNotificationsFromApplication]; - defaultCenter.checkRemoteNotificationAvailability = NO; - defaultCenter.localNotificationHandler = nil; + defaultCenter = [[JRNLocalNotificationCenter alloc] init]; }); return defaultCenter; } -- (void)loadScheduledLocalPushNotificationsFromApplication -{ - NSArray *scheduleLocalPushNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; - for (UILocalNotification *localNotification in scheduleLocalPushNotifications) { +#pragma mark - Initialization + +- (instancetype)init { + self = [super init]; + if (self) { + _mutableScheduledLocalNotificationsByKey = [NSMutableDictionary new]; + _checkRemoteNotificationAvailability = NO; + _localNotificationHandler = nil; + [self loadScheduledLocalPushNotificationsFromApplication]; + } + return self; +} + +#pragma mark - Getters + +- (NSDictionary *)scheduledLocalNotificationsByKey { + return [NSDictionary dictionaryWithDictionary:self.mutableScheduledLocalNotificationsByKey]; +} + +#pragma mark - Private + +- (void)loadScheduledLocalPushNotificationsFromApplication { + NSArray *scheduleLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; + for (UILocalNotification *localNotification in scheduleLocalNotifications) { if (localNotification.userInfo[JRNLocalNotificationHandlingKeyName]) { - [self.localPushDictionary setObject:localNotification forKey:localNotification.userInfo[JRNLocalNotificationHandlingKeyName]]; + [self.mutableScheduledLocalNotificationsByKey setObject:localNotification forKey:localNotification.userInfo[JRNLocalNotificationHandlingKeyName]]; } } } -- (NSArray *)localNotifications -{ - return [[NSArray alloc] initWithArray:[self.localPushDictionary allValues]]; +- (BOOL)isLocalNotificationScheduledForKey:(NSString *)key { + return (self.mutableScheduledLocalNotificationsByKey[key] != nil); } +- (NSDate *)fireDateForExistingScheduledNotificationForKey:(NSString *)key { + UILocalNotification *notification = self.mutableScheduledLocalNotificationsByKey[key]; + if (notification) { + return notification.fireDate; + } + return nil; +} - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo { @@ -55,7 +75,7 @@ - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo if (!key) { return; } - [self.localPushDictionary removeObjectForKey:key]; + [self.mutableScheduledLocalNotificationsByKey removeObjectForKey:key]; [[NSNotificationCenter defaultCenter] postNotificationName:JRNApplicationDidReceiveLocalNotification object:nil @@ -70,7 +90,7 @@ - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo - (void)cancelAllLocalNotifications { [[UIApplication sharedApplication] cancelAllLocalNotifications]; - [self.localPushDictionary removeAllObjects]; + [self.mutableScheduledLocalNotificationsByKey removeAllObjects]; } - (void)cancelLocalNotification:(UILocalNotification *)localNotification @@ -81,19 +101,19 @@ - (void)cancelLocalNotification:(UILocalNotification *)localNotification [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; if (localNotification.userInfo[JRNLocalNotificationHandlingKeyName]) { - [self.localPushDictionary removeObjectForKey:localNotification.userInfo[JRNLocalNotificationHandlingKeyName]]; + [self.mutableScheduledLocalNotificationsByKey removeObjectForKey:localNotification.userInfo[JRNLocalNotificationHandlingKeyName]]; } } - (void)cancelLocalNotificationForKey:(NSString *)key { - if (!self.localPushDictionary[key]) { + if (!self.mutableScheduledLocalNotificationsByKey[key]) { return; } - UILocalNotification *localNotification = self.localPushDictionary[key]; + UILocalNotification *localNotification = self.mutableScheduledLocalNotificationsByKey[key]; [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; - [self.localPushDictionary removeObjectForKey:key]; + [self.mutableScheduledLocalNotificationsByKey removeObjectForKey:key]; } #pragma mark - @@ -256,9 +276,9 @@ - (UILocalNotification *)postNotificationOnNow:(BOOL)presentNow badgeCount:(NSUInteger)badgeCount repeatInterval:(NSCalendarUnit)repeatInterval; { - if (self.localPushDictionary[key]) { + if (self.mutableScheduledLocalNotificationsByKey[key]) { //same key already exists - return self.localPushDictionary[key]; + return self.mutableScheduledLocalNotificationsByKey[key]; } UILocalNotification *localNotification = [[UILocalNotification alloc] init]; @@ -325,7 +345,7 @@ - (UILocalNotification *)postNotificationOnNow:(BOOL)presentNow localNotification.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } - [self.localPushDictionary setObject:localNotification forKey:key]; + [self.mutableScheduledLocalNotificationsByKey setObject:localNotification forKey:key]; return localNotification; } else { return nil;