iOS specific configuration

To enable push notifications for iOS, you need to configure specific capabilities and handle the necessary app delegate code. Let’s walk through these steps.

1

Update iOS pods

Run pod install in the ios folder of your React Native application to install/update the pod dependencies.

2

Add capabilities in iOS application

  1. Open your React Native project in Xcode.

  2. Inside the Targets section, select your app target, and go to Signing & Capabilities.

  3. Click on + Capability and add Push Notifications and Background Modes capabilities.

  4. In Background Modes, select the Remote Notifications option. This is required to receive background push notifications. Learn more about background notifications here.

3

Register for push notification in the AppDelegate file

To handle push notifications in your iOS app, add the following code to the AppDelegate file:

  1. Import the required packages:
1#import "AppDelegate.h"
2#import <React/RCTBundleURLProvider.h>
3#import <UserNotifications/UserNotifications.h>
4#import <fyno/fyno-Swift.h>
  1. If you are using Firebase Cloud Messaging (FCM), add the Firebase imports:
1#import <FirebaseMessaging/FirebaseMessaging.h>
2#import <FirebaseCore/FirebaseCore.h>
  1. Make the required changes in the AppDelegate file
1@interface AppDelegate () <UNUserNotificationCenterDelegate>
2@property (nonatomic, strong) fyno *fynosdk;
3@end
4@implementation AppDelegate
5- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
6{
7 ...
8 UNUserNotificationCenter.currentNotificationCenter.delegate = self.fynosdk;
9 self.fynosdk = [fyno app];
10 [self.fynosdk registerForRemoteNotifications];
11 ...
12}
13- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
14 NSLog(@"Failed to register for remote notifications: %@", error.localizedDescription);
15}
16- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
17 // Send the device token to fynoServer
18 [self.fynosdk setdeviceTokenWithDeviceToken:deviceToken];
19}
20
21...
4

Add a Notification Service Extension

To ensure rich push notifications and proper handling of incoming messages, add a Notification Service Extension to your iOS project:

  1. In Xcode go to File > New > Target.
  2. Select Notification Service Extension from the template list.
  3. Provide a product name, select your development team, choose the appropriate language, and click Finish.

Replace the contents of the generated NotificationService file with the following code:

1#import "NotificationService.h"
2#import <fyno/fyno-Swift.h>
3#import <FirebaseCore/FirebaseCore.h>
4#import <FirebaseMessaging/FirebaseMessaging.h>
5
6@interface NotificationService ()
7 @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
8 @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
9@end
10
11@implementation NotificationService
12 - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
13 [[fyno app] handleDidReceive:request withContentHandler:contentHandler];
14 }
15
16 - (void)serviceExtensionTimeWillExpire {
17 // Called just before the extension will be terminated by the system.
18 // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
19 self.contentHandler(self.bestAttemptContent);
20 }
21@end
5

Add Fyno SDK to the Notification Service Extension

In order for the Notification Service Extension to be able to access fyno, you will have to import it by following the below steps:

  1. In Xcode, select your Notification Service Extension target.
  2. Under Frameworks and Libraries, click on the + button.
  3. Select the framework named fyno.xcframework and click on Add
You have successfully integrated Fyno’s React Native Push Notification SDK into your application. Go ahead and test the integration by sending a push notification using Notification Events