diff --git a/DemoApp/AppDelegate.m b/DemoApp/AppDelegate.m index 5fc9eec..c2d82ef 100644 --- a/DemoApp/AppDelegate.m +++ b/DemoApp/AppDelegate.m @@ -14,7 +14,7 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. - [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)]; + [self registerUserNofication]; application.applicationIconBadgeNumber = 0; [[JRNLocalNotificationCenter defaultCenter] setLocalNotificationHandler:^(NSString *key, NSDictionary *userInfo) { @@ -27,8 +27,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( } }]; - if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { - [[JRNLocalNotificationCenter defaultCenter] didReceiveLocalNotificationUserInfo:launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]]; + UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; + if (localNotification) { + [[JRNLocalNotificationCenter defaultCenter] didReceiveLocalNotification:localNotification]; } return YES; @@ -36,7 +37,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { - [[JRNLocalNotificationCenter defaultCenter] didReceiveLocalNotificationUserInfo:notification.userInfo]; + [[JRNLocalNotificationCenter defaultCenter] didReceiveLocalNotification:notification]; } - (void)applicationWillResignActive:(UIApplication *)application @@ -66,4 +67,24 @@ - (void)applicationWillTerminate:(UIApplication *)application // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } +#pragma mark - Helper + +- (void)registerUserNofication +{ + if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { + UIUserNotificationSettings *settings = [UIApplication sharedApplication].currentUserNotificationSettings; + // check for user notification settings + if (settings.types == UIUserNotificationTypeNone) { + // specify notif types + UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert; + UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types + categories:nil]; + [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; + } + } + else { + [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert]; + } +} + @end diff --git a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h index 9f546c4..a2b7c3c 100644 --- a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h +++ b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.h @@ -21,7 +21,7 @@ typedef void (^JRNLocalNotificationHandler)(NSString *key, NSDictionary *userInf //Handling -- (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo; +- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification; //Cancel diff --git a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m index 2b32a8f..0bd4d66 100644 --- a/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m +++ b/JRNLocalNotificationCenter/JRNLocalNotificationCenter.m @@ -49,8 +49,9 @@ - (NSArray *)localNotifications } -- (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo +- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification { + NSDictionary *userInfo = localNotification.userInfo; NSString *key = userInfo[JRNLocalNotificationHandlingKeyName]; if (!key) { return; @@ -62,7 +63,7 @@ - (void)didReceiveLocalNotificationUserInfo:(NSDictionary *)userInfo userInfo:userInfo]; if (self.localNotificationHandler) { - self.localNotificationHandler(userInfo[JRNLocalNotificationHandlingKeyName], userInfo); + self.localNotificationHandler(key, userInfo); } }