Receiving a notification

  • User.com SDK uses Firebase Cloud Messaging to send push notifications to your users directly from your app.

    Configure FCM

    If you don’t have FCM integrated with your app yet you have to configure Push Notification by visiting https://firebase.google.com/docs/ios/setup. Firebase SDK has been added to your project and it’s configuration is handled by User.com SDK so you don’t have to add it to your app manually or configure it, but

  • Register for notifications

    After configuring FCM, invoke this method in order to register for notifications, it will prompt the user to allow push notifications for your app if he hasn’t already.

    UserSDK.default?.registerForRemoteNotifications(options: [.alert, .badge, .sound])
    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
    [[UserSDK default] registerForRemoteNotificationsWithOptions:options notificationDelegate:self];
  • Handling notifications

    You can register classes to receive notification updates by implementing RemoteNotificationDelegate:

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
  • the following line:

    UserSDK.default?.handleNotification(userInfo: userInfo)
    [[UserSDK default] handleNotificationWithUserInfo:userInfo];
  • After updating the code the SDK will handle in-app messages sent from User.com panel. In case if there is any other notification our SDK receives it allows to register classes to receive updates about those notifications by implementing RemoteNotificationDelegate:

    class MainViewController: TableViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            registerForNotificationsUpdates()
        }
    
    }
    
    extension MyViewController: RemoteNotificationDelegate {
        private func registerForNotificationsUpdates() {
            // register this view controller as a reciver for notifications
            UserSDK.default?.notificationDelegate = self
        }
    
        func didReceiveRemoteNotification(with id: String, data: [AnyHashable : Any]) {
            //handle remote notification
        }
    }
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    
    [[UserSDK default] handleNotificationWithUserInfo:userInfo];
    
    @interface MainViewController () 
    
    @end
    
    @implementation MainViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         // register this view controller as a receiver for notifications
        [UserSDK default].notificationDelegate = self;
    }
    
    
    
    - (void)didReceiveRemoteNotificationWith:(NSString *)id data:(NSDictionary *)data {
        //handle remote notification
    }
    
    @end
  • the following line:

    SDK will use only notifications containing User.com tag, so if you have already FCM in your project, SDK will not interfere with existing notifications.