About this SDK
This Loop Messenger Client SDK for iOS devices includes copy-paste Swift code to embed the Loop Messenger Channel and its Conversation View inside your third-party, customer-facing App. In addition to the core Loop Messenger experience, this SDK also exposes several aspects of the Loop API for convenience.
Features of this SDK include:
- Embedded Loop Messenger Channel
- Support for all OnDemand features
- Support for all Messenger features
- Push Notification support
- Notification Sound support
- App Icon Badge support
- Loop API controls
This SDK is not generally available. For select accounts and as a licensed extension to your existing Loop platform, please inquire with your Benbria representative for access.
Initializing the SDK
In order to start using Loop SDK features, first initialize the Loop SDK when the app launches. In AppDelegate.swift, this can be done in the applicationDidFinishLaunching event:
import UIKit
import Loop
@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
// ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
Loop.initialize(applicationId: APP_ID)
// ...
return true
}
}
Providing Contact Information
Provide the contact information to identify your users who use Loop. If you do not set a contact, your users will appear as anonymous in Loop. Create a LoopContact object as follows. All fields are optional.
var contact = LoopContact()
contact.name = "John Smith"
contact.email = "john.smith@example.com"
contact.phone = "+16135551234"
If you use Loop with a PMS or guest records, you can provide the stay information:
let guestStay = LoopGuestStay(
room: "1260",
arrivalTime: "2018-02-02",
departureTime: "2018-02-05"
)
Then assign this stay to your contact:
contact.addStay(guestStay)
Any time before launching the Conversation View Controller, set this contact to the Loop client:
Loop.setContact(contact)
To load the Contact from a previous session stored on the device, if exists:
var contact: LoopContact? = LoopContact.stored()
Alternatively, you can load a Contact by specifying a contact ID.
let contact = LoopContact(id: "0a7f033a-58e0-4e5c-b9a3-d95462bb67df")
Initializing and Displaying the Conversation View Controller
The Conversation View Controller will use the Contact object set above. If this is a new Contact, Loop will create a new Conversation. If this is an existing Contact, Loop will load the current conversation in the Conversation View Controller.
You can launch the LoopConversationViewController as follows:
Loop.showConversationView()
You can also dismiss the conversation by calling the following method:
Loop.hideConversationView()
Push Notifications Setup
The Loop SDK supports push notifications. This allows your customers to receive push notifications when a team member responds in the conversation. You can add Loop as a second push notifications provider to your app, so your existing push notifications configuration will not be disrupted.
Create a new APNS certificate and upload it to Loop Admin Console along with your app bundle id (e.g. com.mycompany.myapp). For more information on this step, there are many helpful resources online - try searching for articles on “how to create APNs certificates”.
Add push notifications "entitlement" to your app in Xcode
Register for Remote Notifications
Ask for push notifications permission from the user and register the app for APNs. You can do this after the login and every app launch from then on.
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
Let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Receive APNs Device Token and Pass It to Loop
In AppDelegate.swift:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
Loop.setApnsToken(deviceToken)
}
This sends the device token to Loop’s servers. Now your app can start receiving push notifications from Loop.
Handle Notification Press
Your users would like to see the Loop Conversation when they press on the notification. In order to do this, you can capture an event and present the view controller which contains LoopConversationView.
In AppDelegate.swift:
func openLoopConversation() {
// Open your view controller for the conversation view
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if launchOptions != nil{
// ...
let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]
if userInfo != nil && Loop.isLoopNotification(userInfo) {
self.openLoopConversation()
}
// ...
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// ...
if Loop.isLoopNotification(userInfo) {
self.openLoopConversation();
}
completionHandler(UIBackgroundFetchResult.newData)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print(response)
let userInfo = response.notification.request.content.userInfo
if Loop.isLoopNotification(userInfo) {
self.openLoopConversation();
}
}
Notification Sound and App Icon Badge Preferences
By default Loop notifications play a sound and show a badge on your app icon. You can disable this for all users or per user preference:
contact.appIconBadgeAllowed = false
contact.notificationSound = false
contact.save()
Using the Loop API
Loop offers a REST API which you can use with your app ID and guest credentials. The Loop Messenger Client SDK offers a convenient client module for all API endpoints. For instance, you can load the pending OnDemand requests for a guest like the following:
LoopApiClient.getContactRequests(state: .pending) { (requests: [OnDemandRequest], error: Error?)
if error == nil {
requests.forEach { (request) in
print("Request \(request.createdOn)")
request.entries.forEach { (entry) in
print("- \(entry.title): \(entry.quantity)")
}
}
}
}
Comments
0 comments
Article is closed for comments.