Adding push capability

  1. Inside your Target select signing and capabilities
  2. Click on +capabilities and select Push Notifications

Registering for push notifications

AppDelegate should implement UNUserNotificationCenterDelegate from UserNotifications and then add registerForPush method to AppDelegate and call that method inside application(_:didFinishLaunchingWithOptions:).

import UserNotifications // Add this


class AppDelegate: NSObject, UNUserNotificationCenterDelegate { // implement UNUserNotificationCenterDelegate

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    SuprSend.shared.configure(publicKey: "PUBLIC_KEY")
    registerForPush() // add this
    return true
  }

  // add this method
  func registerForPush() {
    UNUserNotificationCenter.current().delegate = self // this willregister push delegate

    // ask user for permission 
    // options = [.sound, .badge, .alert] for explicit authorization
    // options = [.badge, .alert, .sound, .provisional] for provisional authorization
    UNUserNotificationCenter.current().requestAuthorization(
      options: [.sound, .badge, .alert],
      completionHandler: { granted, error in
        if granted {
          DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
          }
        }
      })
  }
}

Asking user permission

Adding delegate methods for push handling

func application(
  _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
  let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
  let token = tokenParts.joined()

  Task {
    await SuprSend.shared.user.addPush(token)
  }
}

func application(
  _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {

  SuprSend.shared.push.application(
    application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler)

  completionHandler(.newData)
}

func userNotificationCenter(
  _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
  withCompletionHandler completionHandler: @escaping () -> Void
) {

  SuprSend.shared.push.userNotificationCenter(
    center, didReceive: response, withCompletionHandler: completionHandler)

  completionHandler()
}

func userNotificationCenter(
  _ center: UNUserNotificationCenter, willPresent notification: UNNotification,
  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {

  SuprSend.shared.push.userNotificationCenter(
    center, willPresent: notification, withCompletionHandler: completionHandler)

  if #available(iOS 14.0, *) {
    completionHandler([.banner, .badge, .sound])
  } else {
    // Fallback on earlier versions
    completionHandler([.alert, .badge, .sound])
  }
}

Changes in Notification Service Extension

Adding Notification Service Extension

  1. In Xcode go to File > New > Target.

  2. Select Notification Service Extension from the template list.

  3. Then in Next popup give it any product name, select your team, select swift language and click finish.

    After clicking on “Finish”, a folder will be created with your given product name.

Installing SuprSend SDK in Notification Service

Adding code in Notification Service

Paste the below code in NotificationService.swift file. Replace YOUR_PUBLIC_KEY with your public key.

import UIKit
import UserNotifications
import SuprSendSwift

class NotificationService: SuprSendNotificationService {
    override func publicKey() -> String { "YOUR_PUBLIC_KEY" }
}

By default SDK will handle only http deeplinks. If you want to handle custom deeplinks, implement SuprSendDeepLinkDelegate in AppDelegate class and add the below code.


// implement SuprSendDeepLinkDelegate
class AppDelegate: NSObject, UNUserNotificationCenterDelegate, SuprSendDeepLinkDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    SuprSend.shared.configure(publicKey: "YOUR_PUBLIC_KEY")
    SuprSend.shared.setDeepLinkDelegate(self)  // Add this
    registerForPush()
    return true
  }

  func shouldHandleSuprSendDeepLink(_ url: URL) -> Bool {
    print("Handling URL: \(url)")  // write your linking logic here and return false
    return false
  }
}


Final AppDelegate.swift file

Example of AppDelegate.swift file with all the above code.

import Foundation
import SuprSend
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate,
  SuprSendDeepLinkDelegate
{

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    SuprSend.shared.enableLogging()
    SuprSend.shared.configure(publicKey: "Testing")
    SuprSend.shared.setDeepLinkDelegate(self)
    registerForPush()
    return true
  }

  func registerForPush() {
    UNUserNotificationCenter.current().delegate = self

    UNUserNotificationCenter.current().requestAuthorization(
      options: [.sound, .badge, .alert],
      completionHandler: { granted, error in
        if granted {
          DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
          }
        }
      })
  }

  func application(
    _ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
  ) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()

    Task {
      await SuprSend.shared.user.addiOSPush(token)
    }
  }

  func application(
    _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
  ) {

    SuprSend.shared.push.application(
      application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler
    )

    completionHandler(.newData)
  }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
    withCompletionHandler completionHandler: @escaping () -> Void
  ) {

    SuprSend.shared.push.userNotificationCenter(
      center, didReceive: response, withCompletionHandler: completionHandler)

    completionHandler()
  }

  func userNotificationCenter(
    _ center: UNUserNotificationCenter, willPresent notification: UNNotification,
    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
  ) {

    SuprSend.shared.push.userNotificationCenter(
      center, willPresent: notification, withCompletionHandler: completionHandler)

    if #available(iOS 14.0, *) {
      completionHandler([.banner, .badge, .sound])
    } else {
      // Fallback on earlier versions
      completionHandler([.alert, .badge, .sound])
    }
  }

  func shouldHandleSuprSendDeepLink(_ url: URL) -> Bool {
    print("Handling URL: \(url)")
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    return false
  }

}