Latest 1.0.3
Homepage https://github.com/nickplee/NPLRemoteNotificationManager
License MIT
Platforms ios 7.0, requires ARC
Dependencies Mantle
Authors

Responding to push notifications in an iOS app usually requires either a) the usage of NSNotificationCenter or b) the maintenance of references to unrelated parts of an application in its delegate. This is probably better because you don’t need to do either of those, nor do you have to pass dictionaries around.

Usage

Subclassing NPLRemoteNotification

You must create a subclass of NPLRemoteNotification for each type of notification you wish to handle. Overriding +keyPathsToNotificationKeys informs the mapping of the notification’s userInfo to your subclass’s properties.

MyNotification.h:

#import <NPLRemoteNotificationManager/NPLRemoteNotification.h>

@interface MyNotification : NPLRemoteNotification

@property (nonatomic, strong) NSString *myProperty;

@end

MyNotification.m:

#import "MyNotification.h"

@implementation MyNotification

+ (NSDictionary *)keyPathsToNotificationKeys
{
    return @{
                @"myProperty" : @"server_property"
             };
}

@end

Configuring Your Application

Registering your subclass

For notification handling to work correctly, you must register your subclasses of NPLRemoteNotification with the NPLRemoteNotificationManager singleton.

Example app delegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    ...

    [[NPLRemoteNotificationManager sharedManager] registerNotificationClass:[MyNotification class]];

    ...

}

Handling Notifications

Simply forward your notifications to the NPLRemoteNotificationManager singleton. The notification’s class will be automatically inferred based on the key paths provided in +keyPathsToNotificationKeys on your NPLRemoteNotification subclass.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    BOOL result = [[NPLRemoteNotificationManager sharedManager] handleNotificationWithUserInfo:userInfo applicationState:[application applicationState]];

    if (!result) {
        NSLog(@"Received unknown notification");
    }
}

Adding Observers

Responding to these notifications is very easy. Simply implement the NPLRemoteNotificationObserver on the class(es) in which the notification is needed and register them with the NPLRemoteNotificationManager singleton.

Example View Controller:

#import "MyViewController.h"
#import <NPLRemoteNotificationManager/NPLRemoteNotificationManager.h>
#import "MyNotification.h"

@implementation MyViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];

    [[NPLRemoteNotificationManager sharedManager] addObserver:self forNotificationClass:[MyNotification class]];
}

- (void)dealloc
{
    [[NPLRemoteNotificationManager sharedManager] removeObserver:self];
}

@end

#pragma mark - NPLRemoteNotificationObserver

- (void)notificationManager:(NPLRemoteNotificationManager *)manager 
       receivedNotification:(NPLRemoteNotification *)notification 
           applicationState:(UIApplicationState)applicationState
{
    if ([notification isKindOfClass:[MyNotification class]]) {
        MyNotification *myNotification = (MyNotification *)notification;

        // Do what you want with it

        NSLog(@"lulzzzzzz: %@", [myNotification myProperty]);
    }
}

Installation

NPLRemoteNotificationManager is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "NPLRemoteNotificationManager"

Author

Nick Lee, [email protected]

License

NPLRemoteNotificationManager is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "NPLRemoteNotificationManager",
    "version": "1.0.3",
    "summary": "An easier way to handle push notification logic around your app.",
    "homepage": "https://github.com/nickplee/NPLRemoteNotificationManager",
    "license": "MIT",
    "authors": {
        "Nick Lee": "[email protected]"
    },
    "source": {
        "git": "https://github.com/nickplee/NPLRemoteNotificationManager.git",
        "tag": "1.0.3"
    },
    "social_media_url": "https://twitter.com/nickplee",
    "platforms": {
        "ios": "7.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes",
    "dependencies": {
        "Mantle": [
            "~> 1.5"
        ]
    }
}

Pin It on Pinterest

Share This