Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alanjcharles authored Aug 7, 2023
1 parent 64ef068 commit b0fe884
Showing 1 changed file with 373 additions and 18 deletions.
391 changes: 373 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,380 @@
# Swift Destination Plugin Template
This template is resolved around `ExampleDestination` (to be replaced by you).

## What does the template provide
### Data class for holding settings
To standardize fetching and using settings in your destination, we recommend using a Coable class to store your destination settings. If marked `Codable`, it will enable you to retrieve your destination settings in a strongly typed construct.
# Twilio Engage Plugin

### Settings-related functions
We provide APIs to update your destination settings in `update(settings:type:)`.
`UpdateType.initial` lets you know if this is the intial or subsequent fetch.
- [Twilio Engage Destination](#twilio-engage-destination)
- [Getting Started](#getting-started)
- [Subscription](#subscription)
- [Additional Setup](#additional-setup)
- [Predefined Actions](#predefined-actions)
- [Handling Notifications](#handling-notifications)
- [Handling Media](#handling-media)
- [Create Extension](#creating-the-extension)
- [Display Media](#displaying-media)
- [License](#license)

`Settings.isDestinationEnabled(key: String)`
- check if your destination is enabled
This plugin enables Segment's Analytics SDK to do push notification management with Twilio Engage.

`Settings.integrationSettings(forKey: String)`
- retrieve a typed destination object
## Getting Started

### Sample implementation for common destination functions
We have templated common destinations functions like `track`, `identify`, `screen`, `group`, `alias` that you should modify to fit your vendor SDK implementation. Although these functions do not need to return the ending payload, its good practice to do so (for unit testing purposes).
To get started:
1. follow the set up instructions for Analytics Swift [here](https://segment.com/docs/connections/sources/catalog/libraries/mobile/kotlin-android/#getting-started)
to integrate Segment's Analytics SDK into your app.
2. add the dependency:

### Transforming events
Often times, destinations need to transform events (eg: change names, modify properties/traits etc.). We have templated an example of transformation in the `track(event:)` example. we recommend you use this approach to perform any such transformations. This will make your code more legible plus improve code quality.
### via Xcode
In the Xcode `File` menu, click `Add Packages`. You'll see a dialog where you can search for Swift packages. In the search field, enter the URL to this repo.

### Testing functions for primary functions (to be expanded)
We have provided a very bare minimum template for testing the primary destination APIs. We recommend to use this as a starter and build upon it to get test coverage of most scenarios.
https://github.com/segment-integrations/analytics-swift-engage

You'll then have the option to pin to a version, or specific branch, as well as which project in your workspace to add it to. Once you've made your selections, click the `Add Package` button.

### via Package.swift

Open your Package.swift file and add the following do your the `dependencies` section:

```
.package(
name: "Segment",
url: "https://github.com/segment-integrations/analytics-swift-engage.git",
from: "1.1.2"
),
```

3. Import the Plugin in the file you configure your analytics instance:

```
import Segment
import TwilioEngage // <-- Add this line
```

4. Just under your Analytics-Swift library setup, call `analytics.add(plugin: ...)` to add an instance of the plugin to the Analytics timeline.

```
let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>")
.flushAt(3)
.trackApplicationLifecycleEvents(true))
let engage = TwilioEngage { previous, current in
print("Push Status Changed /(current)")
}
analytics.add(plugin: engage)
```
## Subscription

Once the plugin is setup, it automatically tracks and updates push notification subscriptions
according to device's notification permissions. To listen to the subscription status changes,
provide a `StatusCallback` when initialize the plugin as following:
```swift
let engage = TwilioEngage { previous, current in
//handle status updates
print("Push Status Changed /(current)")
}
```

On iOS, three different statuses are tracked: `Subscribed`, `DidNotSubscribe`, `Unsubscribed`.
* `Subscribed` is reported whenever app user toggles their device settings to allow push notification
* `DidNotSubscribe` is reported in fresh start where no status has ever been reported
* `Unsubscribed` is reported whenever user toggles their device settings to disable push notification and when the SDK fails to obtain a token from APNS


## Additional Setup

You will also need to add or modify the following methods in your `AppDelegate` in order to start receiving and handling push notifications:

**AppDelegate**

```Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

//add the following:

let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
guard granted else {
Analytics.main.declinedRemoteNotifications()
Tab1ViewController.addPush(s: "User Declined Notifications")
return
}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}

//Necessary in older versions of iOS.
if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Codable] {
Tab1ViewController.addPush(s: "App Launched via Notification \(notification)")
Analytics.main.receivedRemoteNotification(userInfo: notification)
}

...

return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
//Segment event to register for remote notifications
Analytics.main.registeredForRemoteNotifications(deviceToken: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
//Segment event for failure to register for remote notifications
Analytics.main.failedToRegisterForRemoteNotification(error: error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
//Segment event for receiving a remote notification
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

//Handle the notification however you see fit

return .noData
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
let userInfo = response.notification.request.content.userInfo
//Segment event for receiving a remote notification
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

//Handle the notification however you see fit
}
```

## Predefined Actions

Twilio Engage provides 4 predefined `tapActions` that you can handle however you see fit.
* `open_app`: the app opens to the main view when the notification/button is on tapped.
* `open_url`: the default browser opens a webview to the provided `link`
* `deep_link`: the application routes to the provided `link`
* `<custom_action>`: a custom string which can be handled much like a deep-link depending on the use case.

## Handling Notifications

How you handle and display notifications is entirely up to you. Examples for each of the predefined `tapActions` can be found below:

**AppDelegate**

When a push is received by a device the data is available in `didReceiveRemoteNotification` (recieved in background) and `userNotificationCenter` (recieved in foreground) in your AppDelegate

```Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
//Segment event for receiving a remote notification
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

//add a custom method to handle the notification data
handleNotificiation(notification: userInfo, shouldAsk: true)

return .noData
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
//Segment event for receiving a remote notification
Analytics.main.receivedRemoteNotification(userInfo: userInfo)

//add a custom method to handle the notification data
handleNotificiation(notification: userInfo, shouldAsk: false)
}

//...
//an extension for logic for displaying different notification types:
extension AppDelegate {

//Handle the notification bacsed on the `tapAction`
func handleNotificiation(notification: [AnyHashable: Any], shouldAsk: Bool) {
if let aps = notification["aps"] as? NSDictionary {
if let tapAction = aps["category"] as? String {
switch tapAction {
case "open_url":
openWebview(notification: notification, shouldAsk: shouldAsk)
case "deep_link":
openDeepLinkViewController(notification: notification, shouldAsk: shouldAsk)
case "<custom_action>":
// handle a custom action like accept/decline or confirm/cancel
default:
// this will catch `open_app` to open home screen
return
}
}
}
}

//if the notification contains a url and the `tapAction` of `open_url`
//open a webview to display the url in a browser
func openWebview(notification: [AnyHashable : Any], shouldAsk: Bool) {
let webViewController = ProgressWebViewController(nibName: "Main", bundle: Bundle.main)

guard var urlString = notification["link"] as? String else { return }
urlString = urlString.replacingOccurrences(of: "engage://", with: "https://")
guard let url = URL(string: urlString) else { return }

let aps = notification["aps"] as? [AnyHashable: Any]
let alert = aps?["alert"] as? [AnyHashable: Any]
let title = alert?["title"] as? String

//a boolean used to determine if notification was received in foreground
//if so, add an alert to display the notification in the app (rather than lock screen or banner)
if shouldAsk == true, let title = title {
let alert = UIAlertController(
title: "New Product Alert!",
message: title,
preferredStyle: UIAlertController.Style.alert
)

alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertAction.Style.default, handler: { _ in
//Cancel Action
}))
alert.addAction(UIAlertAction(title: "Sure!",
style: UIAlertAction.Style.default,
handler: {(_: UIAlertAction!) in
webViewController.websiteTitleInNavigationBar = true
webViewController.load(url)
webViewController.navigationWay = .push
webViewController.pullToRefresh = true
mainView?.navigationController?.pushViewController(webViewController, animated: true)
}))
mainView?.present(alert, animated: true, completion: nil)
} else {
webViewController.websiteTitleInNavigationBar = true
webViewController.load(url)
webViewController.navigationWay = .push
webViewController.pullToRefresh = true

mainView?.navigationController?.pushViewController(webViewController, animated: true)
}
}

//if the `tapAction` is `deep_link`, parse the `link` for the path you want to display:
private func openDeepLinkViewController(notification: [AnyHashable: Any], shouldAsk: Bool)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let deepLinkString = notification["link"] as? String else { return }
let deepLinkScreen = deepLinkString.replacingOccurrences(of: "engage://", with: "")
let deepLinkVC = storyboard.instantiateViewController(identifier: deepLinkScreen)

let aps = notification["aps"] as? [AnyHashable: Any]
let alert = aps?["alert"] as? [AnyHashable: Any]
let title = alert?["title"] as? String
if shouldAsk == true, let title = title {
let alert = UIAlertController(
title: "New Product Alert!",
message: title,
preferredStyle: UIAlertController.Style.alert
)

alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertAction.Style.default, handler: { _ in
//Cancel Action
}))
alert.addAction(UIAlertAction(title: "Sure!",
style: UIAlertAction.Style.default,
handler: {(_: UIAlertAction!) in
mainView?.navigationController?.pushViewController(deepLinkVC, animated: true)
}))
mainView?.present(alert, animated: true, completion: nil)
} else {
mainView?.navigationController?.pushViewController(deepLinkVC, animated: true)
}
}
}
```

## Handling Media

If you would like to display media in your push notifications, you will need to add a `NotificationService` extension to your app. Reference Apple's documentation for a more detailed overview of [UNNotificationServiceExtension](https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension).

### Creating the Extension

1. in Xcode, go to File -> New -> Target
<img width="561" alt="image" src="https://github.com/segment-integrations/analytics-swift-engage/assets/50601149/a95db4c4-dc21-4033-89a8-b88cf9cbbd2b">

2. search for the `Notification Service Extension`
<img width="715" alt="image" src="https://github.com/segment-integrations/analytics-swift-engage/assets/50601149/8d23555a-4431-4715-877b-52778e8e489d">

3. name the extension `<YourAppName>NotificationService>` and finish the creation process.
4. `<YourAppName>NotificationService/NotificationService>` is where you can add custom logic to handle and display media.

### Displaying Media
**NotificationService `didRecieve` example**

```swift
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

if let bestAttemptContent = bestAttemptContent {
var urlString: String? = nil
let mediaArray: NSArray = bestAttemptContent.userInfo["media"] as! NSArray

if let mediaURLString = mediaArray[0] as? String {
urlString = mediaURLString
}

if urlString != nil, let fileURL = URL(string: urlString!){

guard let mediaData = NSData(contentsOf: fileURL) else {
contentHandler(bestAttemptContent)
return
}

guard let mediaAttachment = UNNotificationAttachment.saveImageToDisk(fileIdentifier: "engage-image.png", data: mediaData, options: nil) else {
contentHandler(bestAttemptContent)
return
}

bestAttemptContent.attachments = [ mediaAttachment ]
}
contentHandler(bestAttemptContent)
}

}

...

//add an extension to `UNNotificationAttachment` to download/save the image
@available(iOSApplicationExtension 10.0, *)
extension UNNotificationAttachment {

static func saveImageToDisk(fileIdentifier: String, data: NSData, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? {
let fileManager = FileManager.default
let folderName = ProcessInfo.processInfo.globallyUniqueString
let folderURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(folderName, isDirectory: true)

do {
try fileManager.createDirectory(at: folderURL!, withIntermediateDirectories: true, attributes: nil)
let fileURL = folderURL?.appendingPathComponent(fileIdentifier)
try data.write(to: fileURL!, options: [])
let attachment = try UNNotificationAttachment(identifier: fileIdentifier, url: fileURL!, options: options)
return attachment
} catch let error {
print("error \(error)")
}

return nil
}
}
```

## License
```
MIT License
Copyright (c) 2021 Segment
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

0 comments on commit b0fe884

Please sign in to comment.