Push notifications¶
Push notifications allow an app that isnโt running in the foreground to notify the user when it has information for them. Push notifications originate from a notification server and are pushed to your app on a userโs device.
Setup Push Notifications¶
By default, Numberly handles UIApplicationDelegate and UNUserNotificationCenterDelegate notifications related methods and forward them to your original delegate.
You can disable this auto integration by setting the automatic_integration
value to false
in your Numberly.plist
. If you do so, you will be in charge of implementing notification-related delegate methods.
Enabling Push Notifications¶
By default, Numberly will register for push notifications each time your app launches. You can disable this auto integration by setting the automatic_notification_registration
value to false
in your Numberly.plist
. If you do so, you will be in charge of enabling push notifications in an other suitable moment in the user journey.
Numberly.push.registerForRemoteNotifications(options: [.alert, .sound, .badge]) { (granted) in
//
}
[Numberly.push registerForRemoteNotifications:@[@(NBLPushAuthorizationOptionAlert),
@(NBLPushAuthorizationOptionSound),
@(NBLPushAuthorizationOptionBadge)]
completionHandler:^(BOOL granted) {
//
}];
Tip
Make sure to enable push notifications each time your app launches to keep the token up to date.
Listening to push related events¶
The Push
module offers some great helpers and callbacks, you just need to set it's delegate
which implements the PushDelegate
protocol.
Numberly.push.delegate = self
Numberly.push.delegate = self;
Advanced integration¶
Reset the badge number¶
You can reset the badge number within your application by calling this method:
Numberly.push.clearBadge()
Numberly.push.delegate = self;
Handling notification-related delegates methods manually¶
First set the UNUserNotificationCenter
delegate:
func application(_ application: UIApplication,
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
- (BOOL)application:(UIApplication *)application
willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
return YES;
}
Then, implement the delegates methods:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Numberly.push.register(token: deviceToken)
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(Numberly.push.presentationOptions(notification: notification))
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
Numberly.push.handleNotification(response: response)
completionHandler()
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Numberly.push registerPushToken:deviceToken];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
completionHandler([Numberly.push presentationOptionsForNotification:notification]);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
[Numberly.push handleNotificationResponse:response];
completionHandler();
}