- Register your app for push notifications
- Subcribe your UIViewController(s) for new push notifications
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request.
PushNotificationHandler is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "PushNotificationHandler"
Then, run the following command:
$ pod install
In your AppDelegate.swift file
import PushNotificationHandler
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
PushNotificationHandler.sharedInstance.registerForPushNotifications(application: application)
PushNotificationHandler.sharedInstance.handleApplicationStartWith(application: application, launchOptions: launchOptions)
PushNotificationHandler.sharedInstance.registerNewAPNSTokenHandler { (tokenData, token, error) -> (Void) in
if let validToken = token {
print("Device Token:", validToken)
} else {
print("Device token error: " + (error?.localizedDescription ?? "Strange things are happening"))
}
}
}
Also implement the methods as following
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
PushNotificationHandler.sharedInstance.application(application, didReceiveRemoteNotification: userInfo)
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
PushNotificationHandler.sharedInstance.application(application, didRegister: notificationSettings)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
PushNotificationHandler.sharedInstance.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
PushNotificationHandler.sharedInstance.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}
In your UIViewController file
import PushNotificationHandler
Your controller must implement PushNotificationSubscriber
protocol
override func viewDidLoad() {
super.viewDidLoad()
PushNotificationHandler.sharedInstance.subscribeForPushNotifications(subscriber: self)
}
deinit {
PushNotificationHandler.sharedInstance.unsubscribeForPushNotifications(subscriber: self)
}
func newPushNotificationReceived(aps: [AnyHashable : Any]) {
// Do something with the push notification
}
That's it!
George Tsifrikas
PushNotificationHandler is available under the MIT license. See the LICENSE file for more info.