iOS Push Integration
Add push notification capability
Inside Targets select signing and capabilities click on +capabilities and search for push notification and select it like below
AppDelegate.h file changes
Add the below code in AppDelegate.h file inside iOS folder
#import <UserNotifications/UserNotifications.h> // Add this
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate> // Add UNUserNotificationCenterDelegate in the already existing line
AppDelegate.m file changes
Call registerForPushNotifications method below the SuprSend sdk initialised code which will register the iOS device for push service
[SuprSend.shared configureWithConfiguration:configuration launchOptions:launchOptions]; // init code which is already added at time of initialisation
[SuprSend.shared registerForPushNotifications]; // Add this
Receiving iOS APNS token sending to backend and listening for push notification and tracking user notification clicks/ dismiss can be done using following snippet of code. Directly copy and paste it at end of the AppDelegate.m file before @end at last line
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSUInteger dataLength = deviceToken.length;
if (dataLength == 0) {
return;
}
const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes;
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for (int i = 0; i < dataLength; ++i) {
[hexString appendFormat:@"%02x", dataBuffer[i]];
}
[SuprSend.shared setPushNotificationTokenWithToken:hexString];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
if (@available(iOS 14.0, *)) {
completionHandler(UNAuthorizationOptionSound | UNNotificationPresentationOptionBanner | UNAuthorizationOptionBadge);
} else {
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler API_AVAILABLE(ios(7.0)){
[SuprSend.shared application:application didReceiveRemoteNotification:userInfo];
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
if ([response isSuprSendNotification]) {
[SuprSend.shared userNotificationCenter:center didReceive:response];
}
}
Development Note
iOS Push notifications only work on real devices so while developing/testing use real device to test it instead of simulators
Updated over 2 years ago