Skip to content

mohsinalimat/ASJPushNotificationManager

 
 

Repository files navigation

ASJPushNotificationManager

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.

About

Super easy setup for push notifications in your iOS app

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 97.9%
  • Ruby 2.1%