|
1 | 1 | ---
|
2 |
| -title: Push Notifications Onboarding |
| 2 | +title: Mobile Push Onboarding |
3 | 3 | plan: engage-premier
|
4 | 4 | ---
|
5 | 5 |
|
6 | 6 | This page walks you through the process of setting up mobile push notifications using Segment, Twilio, and Firebase/Apple Developer.
|
7 | 7 |
|
8 |
| -> info "Before you begin" |
9 |
| -> Push notifications in Engage rely on several dependencies. This page provides a high-level overview of the steps required to set up these dependencies and links to the documentation you'll need to follow to complete setup and configuration. |
| 8 | +> info "Prerequisites" |
| 9 | +> This guide assumes familiarity with Swift and Kotlin and is intended for a developer audience. |
10 | 10 |
|
11 | 11 | ## Overview
|
12 | 12 |
|
13 | 13 | You'll set up push notifications in four steps:
|
14 | 14 |
|
15 |
| -1. [Set up analytics for push notifications](#1-set-up-analytics-for-push-notifications). |
| 15 | +1. [Set up analytics for mobile push](#1-set-up-analytics-for-mobile-push). |
16 | 16 | 2. [Add the Engage SDK plugin](#2-add-the-engage-sdk-plugin).
|
17 | 17 | 3. [Configure push credentials](#3-configure-push-credentials).
|
18 | 18 | 4. [Configure push notifications in Engage](#4-configure-push-notifications-in-engage).
|
19 | 19 |
|
20 |
| -## 1. Set up analytics for push notifications |
| 20 | +## 1. Set up analytics for mobile push |
21 | 21 |
|
22 | 22 | Before you can send push notifications, you'll need to set up analytics. In this step, you'll integrate Segment's mobile SDK into your app.
|
23 | 23 |
|
@@ -66,24 +66,161 @@ Follow these steps to integrate the React Native library:
|
66 | 66 |
|
67 | 67 | For detailed instructions on integrating Analytics for React Native, follow the steps in the [Analytics for React Native getting started section](/docs/connections/sources/catalog/libraries/mobile/react-native#getting-started).
|
68 | 68 |
|
69 |
| -## 2. Add the Engage SDK Plugin |
| 69 | +## 2. Add the Engage SDK plugin |
70 | 70 |
|
71 |
| -Next, you'll add the Engage SDK Plugin to your application. |
| 71 | +Next, you'll add the Engage SDK plugins for both iOS and Android to your application. |
72 | 72 |
|
73 | 73 | ### Instructions for iOS
|
74 | 74 |
|
75 |
| -These are the high-level steps required to add the Engage Plugin for iOS: |
| 75 | +Now that you've integrated Analytics-Swift, follow the steps in this section to add the Engage Plugin for iOS. |
76 | 76 |
|
77 |
| -1. Add the Engage SDK Plugin dependency in your `package.swift` file or using Xcode's Swift packages. |
78 |
| -2. Add or modify the methods in the [Additional setup section](https://github.com/segment-integrations/analytics-swift-engage#additional-setup){:target="_blank"} of Segment's Twilio Engage Plugin documentation. |
| 77 | +#### 2a. Add the Engage SDK plugin dependency |
79 | 78 |
|
80 |
| -The previous steps are required. For detailed instructions on adding the Engage SDK Plugin for iOS, as well as for configuration options, follow the steps in the [getting started section](https://github.com/segment-integrations/analytics-swift-engage#getting-started){:target="_blank"} of Segment's Twilio Engage Plugin documentation on GitHub. |
| 79 | +You can add the Engage SDK plugin using either Xcode or `Package.swift`. |
81 | 80 |
|
| 81 | +**Instructions for adding the plugin with Xcode** |
| 82 | + |
| 83 | +1. In the Xcode `File` menu, click **Add Packages**. |
| 84 | +2. In the Swift packages search dialog, enter the following URL: |
| 85 | + |
| 86 | + ``` |
| 87 | + https://github.com/segment-integrations/analytics-swift-engage |
| 88 | + ``` |
| 89 | +
|
| 90 | +3. You'll then have the option to pin to a version or a specific branch, as well as to the project in your workspace. Once you've made your selections, click `Add Package`. |
| 91 | +
|
| 92 | +**Instructions for adding the plugin with `Package.swift`** |
| 93 | +
|
| 94 | +1. Open the `Package.swift` file and add the following to the `dependencies` section: |
| 95 | +
|
| 96 | +``` |
| 97 | +.package( |
| 98 | + name: "Segment", |
| 99 | + url: "https://github.com/segment-integrations/analytics-swift-engage.git", |
| 100 | + from: "1.1.2" |
| 101 | + ), |
| 102 | +``` |
| 103 | +
|
| 104 | +#### 2b. Import the plugin |
| 105 | +
|
| 106 | +1. Import the plugin in the file where you configure your Analytics instance: |
| 107 | +
|
| 108 | + ``` |
| 109 | + import Segment |
| 110 | + import TwilioEngage // <-- Add this line. |
| 111 | + ``` |
| 112 | +
|
| 113 | +2. After your Analytics-Swift library setup, call `analytics.add(plugin: ...)` to add an instance of the plugin to the Analytics timeline: |
| 114 | +
|
| 115 | + ``` |
| 116 | + let analytics = Analytics(configuration: Configuration(writeKey: "<YOUR WRITE KEY>") |
| 117 | + .flushAt(3) |
| 118 | + .trackApplicationLifecycleEvents(true)) |
| 119 | +
|
| 120 | + let engage = TwilioEngage { previous, current in |
| 121 | + print("Push Status Changed /(current)") |
| 122 | + } |
| 123 | +
|
| 124 | + analytics.add(plugin: engage) |
| 125 | + ``` |
| 126 | +
|
| 127 | +3. Add or modify methods to `AppDelegate`: |
| 128 | +
|
| 129 | +To start receiving and handling push notifications, add or modify the following methods in your `AppDelegate`: |
| 130 | +
|
| 131 | +```swift |
| 132 | + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { |
| 133 | +
|
| 134 | + //Add the following: |
| 135 | +
|
| 136 | + let center = UNUserNotificationCenter.current() |
| 137 | + center.delegate = self |
| 138 | + center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in |
| 139 | + guard granted else { |
| 140 | + Analytics.main.declinedRemoteNotifications() |
| 141 | + Tab1ViewController.addPush(s: "User Declined Notifications") |
| 142 | + return |
| 143 | + } |
| 144 | + DispatchQueue.main.async { |
| 145 | + UIApplication.shared.registerForRemoteNotifications() |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // The following conditional statement is necessary to handle remote notifications in older versions of iOS. |
| 150 | + if let notification = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Codable] { |
| 151 | + Tab1ViewController.addPush(s: "App Launched via Notification \(notification)") |
| 152 | + Analytics.main.receivedRemoteNotification(userInfo: notification) |
| 153 | + } |
| 154 | +
|
| 155 | + ... |
| 156 | +
|
| 157 | + return true |
| 158 | +} |
| 159 | +
|
| 160 | +func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { |
| 161 | + // Segment event to register for remote notifications |
| 162 | + Analytics.main.registeredForRemoteNotifications(deviceToken: deviceToken) |
| 163 | +} |
| 164 | +
|
| 165 | +func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { |
| 166 | + // Segment event for failure to register for remote notifications |
| 167 | + Analytics.main.failedToRegisterForRemoteNotification(error: error) |
| 168 | +} |
| 169 | +
|
| 170 | +func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult { |
| 171 | + // Segment event for receiving a remote notification |
| 172 | + Analytics.main.receivedRemoteNotification(userInfo: userInfo) |
| 173 | +
|
| 174 | + // TODO: Customize notification handling based on the received userInfo. |
| 175 | + // Implement actions or UI updates based on the notification content. |
| 176 | +
|
| 177 | + return .noData |
| 178 | +} |
| 179 | +
|
| 180 | +func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { |
| 181 | + let userInfo = response.notification.request.content.userInfo |
| 182 | + //Segment event for receiving a remote notification |
| 183 | + Analytics.main.receivedRemoteNotification(userInfo: userInfo) |
| 184 | +
|
| 185 | + // TODO: Customize notification response handling based on the received userInfo. |
| 186 | + // Implement actions based on the user's response to the notification. |
| 187 | + // Example: Navigate to a specific screen or perform an action based on the notification. |
| 188 | + |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +The previous steps are required. For configuration options, including subscription statuses and media handling, visit the [getting started section](https://github.com/segment-integrations/analytics-swift-engage#getting-started){:target="_blank"} of Segment's Twilio Engage Plugin documentation on GitHub. |
82 | 193 |
|
83 | 194 | ### Instructions for Android
|
84 | 195 |
|
85 |
| -Follow the instructions in the [getting started section](https://github.com/segment-integrations/analytics-kotlin-engage#getting-started){:target="_blank"} of Segment's Twilio Engage Destination documentation on GitHub. |
| 196 | +Now that you've integrated Analytics for Kotlin, follow these steps to add the Engage Plugin for Android: |
| 197 | + |
| 198 | +1. Add the following to your Gradle dependencies: |
| 199 | + |
| 200 | + ```groovy |
| 201 | + implementation 'com.segment.analytics.kotlin.destinations:engage:<LATEST_VERSION>' |
| 202 | + ``` |
| 203 | +
|
| 204 | +2. Add the following service to the `application` tag of your `AndroidManifest.xml` file: |
| 205 | +
|
| 206 | + ```xml |
| 207 | + <service |
| 208 | + android:name="com.segment.analytics.kotlin.destinations.engage.EngageFirebaseMessagingService" |
| 209 | + android:exported="true"> |
| 210 | + <intent-filter> |
| 211 | + <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> |
| 212 | + <action android:name="com.google.firebase.MESSAGING_EVENT" /> |
| 213 | + </intent-filter> |
| 214 | + </service> |
| 215 | + ``` |
| 216 | +
|
| 217 | +3. Add this plugin to your Analytics instance: |
| 218 | +
|
| 219 | + ```kotlin |
| 220 | + analytics.add(TwilioEngage(applicationContext)) |
| 221 | + ``` |
86 | 222 |
|
| 223 | +The previous steps are required. For configuration options, including subscription statuses and customized actions, visit the [getting started section](https://github.com/segment-integrations/analytics-kotlin-engage#getting-started){:target="_blank"} of Segment's Twilio Engage Destination documentation on GitHub. |
87 | 224 |
|
88 | 225 | ## 3. Configure push credentials
|
89 | 226 |
|
|
0 commit comments