Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion JRNLocalNotificationCenter/JRNLocalNotificationCenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ extern NSString *const JRNApplicationDidReceiveLocalNotification;
typedef void (^JRNLocalNotificationHandler)(NSString *key, NSDictionary *userInfo);

@interface JRNLocalNotificationCenter : NSObject

@property (nonatomic, readonly) NSDictionary<NSString *, UILocalNotification *> *scheduledLocalNotificationsByKey;
@property (nonatomic, copy) JRNLocalNotificationHandler localNotificationHandler;

+ (instancetype)defaultCenter;
- (NSArray *)localNotifications;

//Checking
- (BOOL)isLocalNotificationScheduledForKey:(NSString *)key;
- (NSDate *)fireDateForExistingScheduledNotificationForKey:(NSString *)key;

//Handling
- (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo;
Expand Down Expand Up @@ -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
74 changes: 47 additions & 27 deletions JRNLocalNotificationCenter/JRNLocalNotificationCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,70 @@
NSString *const JRNApplicationDidReceiveLocalNotification = @"JRNApplicationDidReceiveLocalNotification";

@interface JRNLocalNotificationCenter()
@property (nonatomic) NSMutableDictionary *localPushDictionary;
@property (nonatomic) NSMutableDictionary<NSString *, UILocalNotification *> *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<NSString *,UILocalNotification *> *)scheduledLocalNotificationsByKey {
return [NSDictionary dictionaryWithDictionary:self.mutableScheduledLocalNotificationsByKey];
}

#pragma mark - Private

- (void)loadScheduledLocalPushNotificationsFromApplication {
NSArray<UILocalNotification *> *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
{
NSString *key = userInfo[JRNLocalNotificationHandlingKeyName];
if (!key) {
return;
}
[self.localPushDictionary removeObjectForKey:key];
[self.mutableScheduledLocalNotificationsByKey removeObjectForKey:key];

[[NSNotificationCenter defaultCenter] postNotificationName:JRNApplicationDidReceiveLocalNotification
object:nil
Expand All @@ -70,7 +90,7 @@ - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo
- (void)cancelAllLocalNotifications
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self.localPushDictionary removeAllObjects];
[self.mutableScheduledLocalNotificationsByKey removeAllObjects];
}

- (void)cancelLocalNotification:(UILocalNotification *)localNotification
Expand All @@ -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 -
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down