Intercepting notifications
It is possible to get the raw payload of a notification (which includes your 'custom payload'), would you wish to do something with it. This is done using standard iOS APIs:
iOS 10
It's recommended to implement the UNUserNotificationCenterDelegate in a class.
It allows you to:
Unify local and remote notifications callbacks
Get the same callback for all user interactions with a push (open, dismiss and actions), even for a cold start
Get notified of notifications received while your app is in the foreground. This allows you to take immediate action or to tell iOS to display the push as if your app was opened in background.
You'll also need to call Batch in its two methods to make sure all of the SDK and dashboard's features work correctly. Keep in mind that being a delegate, it will need to be retained by a variable, since iOS only weakly retains it.
Here's a sample implementation:
@objc
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
[your code]
BatchPush.handle(userNotificationCenter: center, didReceive: response)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
[your code]
BatchPush.handle(userNotificationCenter: center, willPresent: notification, willShowSystemForegroundAlert: true)
// Since you set willShowSystemForegroundAlert to true, you should call completionHandler([.alert, .sound, .badge])
// If set to false, you'd call completionHandler([])
}
}// NotificationDelegate.h
@import Foundation;
@import UserNotifications;
@interface NotificationDelegate : NSObject <UNUserNotificationCenterDelegate>
@end
// NotificationDelegate.m
#import "NotificationDelegate.h"
@import Batch;
@implementation NotificationDelegate
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler {
[your code]
[BatchPush handleUserNotificationCenter:center
didReceiveNotificationResponse:response];
completionHandler();
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[your code]
[BatchPush handleUserNotificationCenter:center
willPresentNotification:notification
willShowSystemForegroundAlert:YES];
// Since you set willShowSystemForegroundAlert to true, you should call completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound)
// If set to false, you'd call completionHandler(0)
}
@endFinally, set this class as your default UNUserNotificationCenter delegate:
Handling a custom payload
The custom payload is merged at the root of the userInfo you get when called back by iOS:
iOS 9 and lower
If your app supports background refresh, it's recommended to implement this method:
Don't forget to call the completionHandler with an appropriate value. Batch will only do so for you when it encounters a deeplink.
Otherwise, if you support iOS 6 devices or don't implement background refresh, please implement the older notification delegate method. Note that both can be implemented for backward-compatibility, you'll only be called on one of them.
Your custom payload will be in the userInfo dictionary.
Handling a custom payload
The custom payload is merged at the root of the userInfo you get when called back by iOS:
Overriding Batch's Deeplink Handling
By default, Batch will automatically try to open the deeplink you've set in your push campaigns.
If you'd like to prevent Batch from doing that while still being able to use deeplinks in push campaigns, you can call the following method in applicationDidFinishLaunchingWithOptions:
Then, you can ask Batch to give you the deeplink from the notification when you get it:
Be careful, this method will return nil if a deeplink could not be found.
Last updated

