Fyno iOS SDK
The Fyno iOS SDK enables you to utilize Fyno's services within your iOS applications, offering tools to manage remote notifications, among other features. Here are the instructions to integrate the Fyno SDK with your native iOS app using Swift.
Requirements
- Fyno Account
- Fyno App ID, available in Settings > Keys & IDs
- iOS 14+ or iPadOS 14+ device (iPhone, iPad, iPod Touch) for testing. Xcode 14+ simulator running iOS 16+ also works.
- Mac with Xcode 12+
- p8 Authentication Token
- Your Xcode Project Should can target any Apple Device excluding the Mac
Installation
Step 1: Add a Notification Service Extension
The FynoNotificationServiceExtension enables your iOS app to receive rich notifications with images, buttons, badges, and other features. It is also essential for Fyno's analytics capabilities.
- In Xcode, select
File > New > Target...
- Choose
Notification Service Extension
, then pressNext
.
- Enter the product name as
FynoNotificationServiceExtension
and pressFinish
.
- Do not Select
Activate
on the ensuing dialog.
- Press
Cancel
on theActivate scheme
prompt. This step keeps Xcode debugging your app, rather than the extension you just created. If you accidentally activated it, you could switch back to debug your app within Xcode (next to the play button).
- In the project navigator, select the top-level project directory and pick the
FynoNotificationServiceExtension
target in the project and targets list. - Ensure the Deployment Target is the same value as your Main Application Target. It should be set to at least iOS 10, the version of iOS that Apple released Rich Media for push. iOS versions under 10 will not support Rich Media.
- In the project navigator, click the
FynoNotificationServiceExtension
folder and open theNotificationService.m
orNotificationService.swift
and replace the entire file's contents with the provided code. Ignore any build errors at this point. We will import Fyno which will resolve these errors.
import UserNotifications
import fyno
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
fynoService.handleDidReceive(request, withContentHandler: contentHandler)
}
override func serviceExtensionTimeWillExpire() {
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}
This code represents a 'NotificationService' class, which is an 'UNNotificationServiceExtension'. An 'UNNotificationServiceExtension' is used to intercept and modify incoming remote push notifications before they're displayed to the user. This is especially useful when you need to add rich content to notifications, such as media attachments, or decrypt encrypted notification content.
Step 2: Import the Fyno SDK into your Xcode project
- The fyno SDK can be added as a Swift Package (compatible with Objective-C as well). Check out the instructions on how to import the SDK directly from Xcode using Swift Package Manager.
- Add the Fyno SDK under Fyno Extension Service in order to enable for Fyno SDK to be handle and handle background/rich Push notifications via the service extension.
Step 3: Add Required Capabilities
This step ensures that your project can receive remote notifications. Apply these steps only to the main application target and not for the Notification Service Extension.
- Select the root project > your main app target and "Signing & Capabilities".
- If you do not see
Push Notifications
enabled, click+ Capability
and addPush Notifications
.
3. Click
+ Capability
and add Background Modes
. Then check Remote notifications
.
Step 4: Add the Fyno Initialization Code
Direct Implementation
Navigate to your AppDelegate file and add the Fyno initialization code.
import Foundation
import UIKit
import fyno
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
let fynosdk = fyno.app
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
fynosdk.requestNotificationAuthorization { granted in
if granted {
DispatchQueue.main.async {
self.fynosdk.registerForRemoteNotifications()
}
}
}
fynosdk.enableTestMode(testEnabled: false)
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
fynosdk.handleRemoteNotification(userInfo: userInfo, fetchCompletionHandler: completionHandler)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Send the device token to fynoServer
let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
fynosdk.initializeApp(WSID: YOUR_WORKSPACE_ID,api_key: YOUR_API_KEY, integrationID: YOUR_INTEGRATION_ID, deviceToken: token){
result in
switch result{
case .success(_):
self.fynosdk.createUserProfile(distinctID: "your_database_unique_identifier",name: "John Doe"){result in
switch result{
case .success(let success):
print(success)
case .failure(let error):
print(error)
}
}
case .failure(let error):
print(error)
}
}
}
// DELETE USER PROFILE
//SIGNOUT
/**
fynosdk.deleteProfile(name: anonymous_profile_name){result in
switch result{
case .success(let success):
print(success)
case .failure(let error):
print(error)
}
}
**/
}
Fyno iOS SDK Implementation Step by Step Guide
The Fyno iOS SDK allows you to leverage Fyno's services in your iOS applications. It provides you with a set of tools to handle remote notifications, among other features.
Installation
The SDK can be found at the following GitHub repository:
https://github.com/fynoio/ios-sdk.git
Initial Setup
- Import
UserNotifications
,UIKit
in the Swift file where you want to use the Fyno SDK.
import UserNotifications
import UIKit
- Initialize an instance of the Fyno class and set it as the delegate for the User Notification Center.
let fynoInstance = fyno.app
UNUserNotificationCenter.current().delegate = fynoInstance
Request Notification Authorization
Use the requestNotificationAuthorization
method to ask the user for notification permissions. This function accepts a closure that takes a Boolean parameter, which indicates whether permission was granted.
fynoInstance.requestNotificationAuthorization { (granted) in
if granted {
// Permission granted
} else {
// Permission not granted
}
}
Register for Remote Notifications
Use the registerForRemoteNotifications
function to register the app for receiving remote notifications.
fynoInstance.registerForRemoteNotifications()
Handle Notifications
The SDK provides several methods to handle notifications:
handleRemoteNotification
: This method is used to handle a remote notification. It accepts the notification's user info and a completion handler.
fynoInstance.handleRemoteNotification(userInfo: userInfo) { (fetchResult) in
// Handle fetch result
}
userNotificationCenter(_:willPresent:withCompletionHandler:)
: This method is called when a notification is received while the app is active.
fynoInstance.userNotificationCenter(center, willPresent: notification) { (options) in
// Handle presentation options
}
userNotificationCenter(_:didReceive:withCompletionHandler:)
: This method is called when a user interacts with a notification, such as clicking or dismissing it.
fynoInstance.userNotificationCenter(center, didReceive: response) {
// Handle user response
}
Utilities
The SDK also provides a Utilities class with the following static methods:
-
downloadImageAndAttachToContent(from:content:completion:)
: This method downloads an image from a URL, attaches it to aUNMutableNotificationContent
instance, and calls a completion handler with the updated content. -
createUserProfile(payload:completionHandler:)
: This method creates a user profile using aPayload
instance and sends a POST request to the server. It calls a completion handler with the result.
Step 5: Initialize/Connect Fyno sdk
This function is crucial to connect with our Fyno application. It needs to be called before creating a profile when the app is starting up with the following configuration:
fynosdk.initializeApp(WSID: WSID,api_key: api_key, integrationID: integrationID, deviceToken: AppDelegate.device_token){
result in
switch result{
case .success(let success):
print(success)
CompletionHandler(.success(success))
case .failure(let error):
print(error)
CompletionHandler(.failure(error))
}
}
}
Step 6: Create user profile for targeting
The createUserProfile(payload:completionHandler:)
method creates a user profile using a Payload
instance and sends a POST request to the server. It calls a completion handler with the result.
fynosdk.createUserProfile(distinctID: (result?.user.email)!,name: (result?.user.displayName)!){result in
switch result{
case .success(let success):
print(success)
case .failure(let error):
print(error)
}
}
Step 7: Delete user profile
The deleteProfile()
method deletes an existing profile using a Payload
instance and sends a POST request to the server. It calls a completion handler with the result.
fynosdk.deleteProfile(name: currentUser){result in
switch result{
case .success(let success):
print(success)
case .failure(let error):
print(error)
}
}
Step 8: Switch to test Environment
The fynosdk.enableTestMode(testEnabled:)
method allows the user to switch from the default 'live' environment to 'test' mode within the Fyno application.
fynosdk.enableTestMode(testEnabled: true)
How to Implement Fyno Inapp in an iOS Application
This guide provides step-by-step instructions on how to integrate the Fyno Inapp service into an iOS application using SwiftUI. In this example, we'll be working with a simple application named demo-ios-sdk-1
.
Steps to Implement Fyno Inapp
- Import the Fyno Inapp SDK: In your SwiftUI file, typically
ContentView.swift
, import the Fyno Inapp SDK at the top of your file. This is done with the lineimport fyno
.
import SwiftUI
import fyno
-
Initialize the Fyno Inapp instance: Next, within your view struct (in this case,
struct ContentView: View
), initialize the Fyno Inapp instance. For this, you'll need to provide:inappUserId
: This is the unique identifier for the user in your application.integrationToken
: This is the token provided by Fyno Inapp for integration purposes.
struct ContentView: View {
let fynoinapp = fynoInapp(inappUserId: "kchandawat-1", signature: "e214cf26-4b04-4be3-b0b7-dac2fd3dfa78")
...
}
- Use the Fyno Inapp instance: Now that the Fyno Inapp instance is set up, you can use it in your views. In this example, we are passing the
fynoinapp
instance to aNotificationList
view which would handle the in-app notifications.
var body: some View {
NotificationList(inappManager: fynoinapp)
}
- Preview your content view: You can preview your content view using the
PreviewProvider
protocol.
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Troubleshooting
If you encounter issues, see our iOS troubleshooting guide. Try the example project on our Github repository. If stuck, contact support directly or email [email protected] for help. For faster assistance, please provide:
- Your Fyno secret key
- Details, logs, and/or screenshots of the issue.