Using CocoaPods:
In your Podfile add the following dependencies:
pod 'PrecisoAi'
You can also add the framework manually to the project.
In the case of our SDK, these are the steps to manually add it to your project:
Import the PrecisoAi framework:
import PrecisoAi
In your didFinishLaunchingWithOptions method implementation, set your license code and the unique identifier you use in the app for the user:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
PrecisoAi.sharedInstance.start(with: application, andLicense: "YOUR_LICENSE_CODE")
//If your user is not yet logged in, you can call this method from your login code
PrecisoAi.sharedInstance.setUserId("YOUR_UNIQUE_USER_IDENTIFIER")
}
That unique identifier should be the one that allows you to identify that user and device uniquely later in our system. If your user is not logged in yet, you can call this method from your login code
In the didRegisterForRemoteNotificationsWithDeviceToken method, pass the obtained token to the SDK:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
PrecisoAi.sharedInstance.setPushToken(deviceToken)
}
Do the same for the method performFetchWithCompletionHandler and call the matching SDK method:
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
PrecisoAi.sharedInstance.application(application, performFetchWithCompletionHandler: completionHandler)
}
Also for the method application:didReceiveRemoteNotification:fetchCompletionHandler:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
PrecisoAi.sharedInstance.application(application, didReceiveRemoteNotification: userInfo, fetchCompletionHandler: completionHandler) {}
}
And add the required background modes to your app adding this lines your Info.plist file:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
<string>remote-notification</string>
</array>
Instead of editing the file manually you can also add this modes using the XCode editor going to the Capabilities tab in you project and adding the three corresponding background modes.
Register for push notifications if your are not doing it already. You must call this method from your didFinishLaunchingWithOptions method or willFinishLaunchingWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
PrecisoAi.sharedInstance.registerForRemoteNotifications()
}
And also ask the user for permission to show push notifications if your are not doing it already. You can call this method from your didFinishLaunchingWithOptions method or willFinishLaunchingWithOptions, or from another part in your code (like after a welcome screen that explains why you want to show notifications to the user):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
PrecisoAi.sharedInstance.requestUserPermissionToShowNotifications()
}
If you are doing the registration of the push notifications on your own, in your method userNotificationCenter(_:didReceive:withCompletionHandler:) of the UserNotifications framework you need to call a SDK method:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
PrecisoAi.sharedInstance.userNotificationCenter(center, didReceive: response)
}
If you want to use custom URLs that open a particular screen of your app, you need to implement this method in your app delegate (modify it for the URL scheme you want to use):
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
//Example implementation:
if url.scheme == "precisoai" {
let userUrl = UserDefaults().value(forKey: "notificationsUrl")
if let url = URL(string: userUrl as! String) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(String(describing: userUrl)): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(String(describing: userUrl)): \(success)")
}
}
return true
}
return false
}
You also need to modify your Info.plist file and add this lines (modify it for the URL scheme you want to use):
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>es.tid.precisoai.deeplink</string>
<key>CFBundleURLSchemes</key>
<array>
<string>precisoai</string>
</array>
</dict>
</array>
Set the quiet hours on the SDK so it never posts notifications during that hours (for example at night):
This could help in avoiding mistakes if you set the wrong timeout period for a notification
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
PrecisoAi.sharedInstance().enableQuietHours(from: 23, to: 8)
}
To be able so show attachments in the push notifications (like an image) you need to add a notification service extension to your app:
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
print("didReceive:withContentHandler: \(request.content.userInfo)")
if let bestAttemptContent = bestAttemptContent {
if let urlString = request.content.userInfo["snAttachmentUrl"], let fileUrl = URL(string: urlString as! String) {
// Download the attachment from the URL
URLSession.shared.downloadTask(with: fileUrl) { (location, response, error) in
if let location = location {
let tmpDirectory = NSTemporaryDirectory()
let tmpFile = "file://".appending(tmpDirectory).appending(fileUrl.lastPathComponent)
let tmpUrl = URL(string: tmpFile)!
do {
try FileManager.default.moveItem(at: location, to: tmpUrl)
} catch {}
// Add the attachment to the notification content
if let attachment = try? UNNotificationAttachment(identifier: "snAttachment", url: tmpUrl) {
bestAttemptContent.attachments = [attachment]
}
}
// Serve the notification content
contentHandler(bestAttemptContent)
}.resume()
}
}