Last active
June 11, 2016 15:11
-
-
Save joshavant/3cb89db0eb0ff65cd5b93382e0d0e552 to your computer and use it in GitHub Desktop.
Optimal Push Notification Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppDelegate: UIResponder, UIApplicationDelegate { | |
private var currentUserNotificationTypes: UIUserNotificationType = .None | |
private func processUpdatedUserNotificationSettings(application: UIApplication, currentUserNotificationSettings notificationSettings: UIUserNotificationSettings?) { | |
let newUserNotificationTypes: UIUserNotificationType = notificationSettings?.types ?? .None | |
// only performs actions if the current value has a delta with the new value | |
if currentUserNotificationTypes.contains(.Alert) != newUserNotificationTypes.contains(.Alert) { | |
self.currentUserNotificationTypes = newUserNotificationTypes | |
if newUserNotificationTypes.contains(.Alert) { | |
application.registerForRemoteNotifications() | |
} else { | |
self.alertDisplayingServices.forEach({ $0.applicationLostAlertNotificationsCapability() }) | |
} | |
} | |
} | |
// MARK: - UIApplicationDelegate | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// updates cached permission settings + gets a fresh APNS token, if needed | |
self.processUpdatedUserNotificationSettings(application, currentUserNotificationSettings: application.currentUserNotificationSettings()) | |
... | |
} | |
func applicationWillEnterForeground(application: UIApplication) { | |
self.processUpdatedUserNotificationSettings(application, currentUserNotificationSettings: application.currentUserNotificationSettings()) | |
} | |
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { | |
self.processUpdatedUserNotificationSettings(application, currentUserNotificationSettings: notificationSettings) | |
} | |
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { | |
if self.currentUserNotificationTypes.contains(.Alert) { | |
self.alertDisplayingServices.forEach({ $0.applicationGainedAlertNotificationsCapability(remoteNotificationDeviceToken: deviceToken) }) | |
} | |
} | |
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { | |
if self.currentUserNotificationTypes.contains(.Alert) { | |
self.alertDisplayingServices.forEach({ $0.applicationLostAlertNotificationsCapability() }) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this, just issue a
UIApplication.sharedApplication().registerUserNotificationSettings:
call within your application. This will trigger a request for the appropriate permissions from the user, and the rest of the pattern will take over from there (ending in the appropriate notifications being sent toself.alertDisplayingServices
).