First, import nudge to the file in which you would like to initialize nudge
In ViewController.swift:
| using Cocoapods | using Swift Package Manager |
import nudge |
import Nudge |
Initialize the nudge® library
We recommend initializing the library as soon as possible from when the app is launched. This is typically done in the file that acts as the entry point for your app. This can be your AppDelegate, SceneDelegate or UIViewController depending on your app’s architecture
| Basic Implementation | Location-Aware Implementation |
var myNudge: Nudge! let apiKey: String = “<YOUR_API_KEY_HERE>" myNudge = Nudge(options: [ “nudgeVersion”: Nudge.NudgeVersion.nudgeStandard, "apiKey": self.apiKey, ]); |
var myNudge: Nudge! let apiKey: String = “<YOUR_API_KEY_HERE>" myNudge = Nudge(options: [ “nudgeVersion”: Nudge.NudgeVersion.nudgeGeo, "apiKey": self.apiKey, ]); |
Parameters:
-
nudgeVersion(Nudge.NudgeVersion): Required* - Determines which version of nudge will be used by the library- Two options: Nudge.NudgeVersion.NUDGE_STANDARD and Nudge.NudgeVersion.NUDGE_GEO
- As of Nudge version
4.1.1, if your application is made using .NET, Objective-C or another C-based platform that struggles with the use of string-based enums, you can instead use anIntorNSNumberfor this field. Use0for STANDARD and1for GEO. - * If you are updating a legacy library implementation this parameter could be skipped, but we recommend refactoring your integration to take full advantage of the new library.
-
apiKey(String): Required - This is your Larky-provided nudge® API key, and is unique for each client integration. Do not re-use API keys! If you are performing an integration for a new client please contact us for a fresh API key.
Handling incoming notifications
Once users receive a push, we need to record some information about it and potentially redirect the user to see the correct content.
Copy this next method under the one you just added:
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) ->Void) {
Nudge.receivedPush(notificationPayload:userInfo, application: application)
}NOTE: If your app already has push notifications, you may want to only call the receivedPush function when a nudge notification was sent. Here's what that looks like:
let isNudge = userInfo["isNudge"] as! Bool;
if (isNudge) {
Nudge.receivedPush(notificationPayload:userInfo, application: application)}
else {
// Do application logic for your notifications here!
}Handling tapped notifications
In order to handle notification tapping we need to add the following after your class AppDelegate: UIResponder, UIApplicationDelegate {} class.
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {
// Receive displayed notifications for iOS 10 devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if#available(iOS 14.0, *) {
completionHandler([.banner, .sound, .badge])
} else {
// Fallback on earlier versions
completionHandler([.alert, .sound, .badge])
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
Nudge.tappedNotification(notification: response.notification)
completionHandler()
}
}Handling rich push notifications
Go to the project root and select the target of the notification service extension you created previously.
Under the General Tab, navigate to the Frameworks and Libraries section and add the Nudge library to your notification service extension target.
In the Project Viewer, navigate to the file NotificationService.swift.
At the top of the file where you place your import statements add the following line:
import NudgeIn the didReceive function add the following code:
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
guard let bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) else {
contentHandler(request.content)
return
}
let userInfo = request.content.userInfo
if let isNudge = userInfo["isNudge"] as? Bool, isNudge == true {
Nudge.handleRichPushNotification(request: request, contentHandler: contentHandler)
} else {
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
contentHandler(bestAttemptContent)
}
return
}
Comments
0 comments
Article is closed for comments.