Latest 1.1
Homepage https://github.com/sudeepjaiswal/ASJPushNotificationManager
License MIT
Platforms ios 7.0, requires ARC
Authors

There is a lot of setup required to enable push/remote notifications in your app. You have to create certificates on the Apple developer portal and register for them in your project. I personally find that clutters up my AppDelegate.

The procedure to setup push notifications changed from iOS 8.0, and if your app still supports versions below, it means more code. This library abstracts away the code for registering device for push notifications and handles the delegate methods for you as well.

Installation

CocoaPods is the recommended way to install this library. Just add the following command in your Podfile and run pod install:

pod 'ASJPushNotificationManager'

Usage

You need to import ASJPushNotificationManager.h in the class where you need to ask the user permission to send them pushes.

+ (instancetype)sharedInstance;

This class is a singleton and you are required to use its shared instance to access the defined properties and methods.

- (void)registerWithTypes:(ASJPushNotificationType)types categories:(nullable NSSet<UIUserNotificationCategory *> *)categories completion:(nullable CompletionBlock)completion;

Call this method to invoke the registration flow. When called for the first time, it will prompt the user that the app would like to send push notifications. The completion block will fire after the user makes a choice, and you will receive the device token or error object.

The default delegate method application:didRegisterForRemoteNotificationsWithDeviceToken: returns the device token as NSData. It is converted into a usable NSString before you receive it in the block. You can pass this string on to your server like always. Note that you will not get a token on simulator.

@property (nullable, readonly, copy, nonatomic) NSString *deviceToken;

The generated device token is always available for use as an exposed property. Note that it can be nil, in case user did not allow receiving push notifications.

- (void)unregister;

To stop receiving pushes in app, you can unregister.

Handling push events

Most of the delegate methods are abstracted away and will not be called even if you write them in AppDelegate (See below). You will need to observe NSNotifications for the different events you are interested in. Just be sure to remove your observers in dealloc:

extern NSString *const ASJUserNotificationSettingsNotification;

Posted when application:didRegisterUserNotificationSettings: is called.

extern NSString *const ASJTokenErrorNotification;

Notification posted when application:didFailToRegisterForRemoteNotificationsWithError: is called.

extern NSString *const ASJTokenReceivedNotification;

Notification posted when application:didRegisterForRemoteNotificationsWithDeviceToken: is called.

extern NSString *const ASJPushReceivedNotification;

Notification posted when application:didReceiveRemoteNotification: is called.

Limitations

I have used the notifications pattern so that the user could receive pushes in any ViewController, hoping it would be easier that way. However, I have been unable to make the methods with completionHandler: blocks work to my satisfaction. So for now, they will be called in AppDelegate. You must call the completionHandler: in these methods for them to work:

Called in AppDelegate

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler;

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler;

Not called in AppDelegate

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings;

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error;

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

Credits

To-do

  • Add checks for iOS 7 so that there are no warnings.

License

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

Latest podspec

{
    "name": "ASJPushNotificationManager",
    "version": "1.1",
    "platforms": {
        "ios": "7.0"
    },
    "license": {
        "type": "MIT"
    },
    "homepage": "https://github.com/sudeepjaiswal/ASJPushNotificationManager",
    "authors": {
        "Sudeep Jaiswal": "[email protected]"
    },
    "summary": "Super easy setup for push notifications in your iOS app",
    "source": {
        "git": "https://github.com/sudeepjaiswal/ASJPushNotificationManager.git",
        "tag": "1.1"
    },
    "source_files": "ASJPushNotificationManager/*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This