-
Notifications
You must be signed in to change notification settings - Fork 399
/
MainActivity.kt
213 lines (191 loc) · 7.47 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.google.firebase.quickstart.dynamiclinks.kotlin
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.appinvite.FirebaseAppInvite
import com.google.firebase.dynamiclinks.PendingDynamicLinkData
import com.google.firebase.dynamiclinks.ShortDynamicLink
import com.google.firebase.dynamiclinks.ktx.androidParameters
import com.google.firebase.dynamiclinks.ktx.component1
import com.google.firebase.dynamiclinks.ktx.component2
import com.google.firebase.dynamiclinks.ktx.dynamicLink
import com.google.firebase.dynamiclinks.ktx.dynamicLinks
import com.google.firebase.dynamiclinks.ktx.googleAnalyticsParameters
import com.google.firebase.dynamiclinks.ktx.iosParameters
import com.google.firebase.dynamiclinks.ktx.itunesConnectAnalyticsParameters
import com.google.firebase.dynamiclinks.ktx.shortLinkAsync
import com.google.firebase.dynamiclinks.ktx.socialMetaTagParameters
import com.google.firebase.ktx.Firebase
import com.google.firebase.quickstart.dynamiclinks.R
abstract class MainActivity : AppCompatActivity() {
private val TAG = "fdl.MainActivity"
// [START on_create]
override fun onCreate(savedInstanceState: Bundle?) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// [END_EXCLUDE]
// [START get_deep_link]
Firebase.dynamicLinks
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData: PendingDynamicLinkData? ->
// Get deep link from result (may be null if no link is found)
var deepLink: Uri? = null
if (pendingDynamicLinkData != null) {
deepLink = pendingDynamicLinkData.link
}
// Handle the deep link. For example, open the linked
// content, or apply promotional credit to the user's
// account.
// ...
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
// [END get_deep_link]
}
// [END on_create]
fun createDynamicLink_Basic() {
// [START create_link_basic]
val dynamicLink = Firebase.dynamicLinks.dynamicLink {
link = Uri.parse("https://www.example.com/")
domainUriPrefix = "https://example.page.link"
// Open links with this app on Android
androidParameters { }
// Open links with com.example.ios on iOS
iosParameters("com.example.ios") { }
}
val dynamicLinkUri = dynamicLink.uri
// [END create_link_basic]
}
fun createDynamicLink_Advanced() {
// [START create_link_advanced]
val dynamicLink = Firebase.dynamicLinks.dynamicLink { // or Firebase.dynamicLinks.shortLinkAsync
link = Uri.parse("https://www.example.com/")
domainUriPrefix = "https://example.page.link"
androidParameters("com.example.android") {
minimumVersion = 125
}
iosParameters("com.example.ios") {
appStoreId = "123456789"
minimumVersion = "1.0.1"
}
googleAnalyticsParameters {
source = "orkut"
medium = "social"
campaign = "example-promo"
}
itunesConnectAnalyticsParameters {
providerToken = "123456"
campaignToken = "example-promo"
}
socialMetaTagParameters {
title = "Example of a Dynamic Link"
description = "This link works whether the app is installed or not!"
}
}
// [END create_link_advanced]
}
private fun processShortLink(shortLink: Uri?, previewLink: Uri?) {
}
fun createShortLink() {
// [START create_short_link]
val shortLinkTask = Firebase.dynamicLinks.shortLinkAsync {
link = Uri.parse("https://www.example.com/")
domainUriPrefix = "https://example.page.link"
// Set parameters
// ...
}.addOnSuccessListener { (shortLink, flowchartLink) ->
// You'll need to import com.google.firebase.dynamiclinks.ktx.component1 and
// com.google.firebase.dynamiclinks.ktx.component2
// Short link created
processShortLink(shortLink, flowchartLink)
}.addOnFailureListener {
// Error
// ...
}
// [END create_short_link]
}
fun shortenLongLink() {
// [START shorten_long_link]
val shortLinkTask = Firebase.dynamicLinks.shortLinkAsync {
longLink = Uri.parse(
"https://example.page.link/?link="
"https://www.example.com/&apn=com.example.android&ibn=com.example.ios",
)
}.addOnSuccessListener { (shortLink, flowChartLink) ->
// You'll need to import com.google.firebase.dynamiclinks.ktx.component1 and
// com.google.firebase.dynamiclinks.ktx.component2
// Short link created
processShortLink(shortLink, flowChartLink)
}.addOnFailureListener {
// Error
// ...
}
// [END shorten_long_link]
}
fun buildShortSuffix() {
// [START ddl_short_suffix]
val shortLinkTask = Firebase.dynamicLinks.shortLinkAsync(ShortDynamicLink.Suffix.SHORT) {
// Set parameters
// ...
}
// [END ddl_short_suffix]
}
fun shareLink(myDynamicLink: Uri) {
// [START ddl_share_link]
val sendIntent = Intent().apply {
val msg = "Hey, check this out: $myDynamicLink"
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, msg)
type = "text/plain"
}
startActivity(sendIntent)
// [END ddl_share_link]
}
fun getInvitation() {
// [START ddl_get_invitation]
Firebase.dynamicLinks
.getDynamicLink(intent)
.addOnCompleteListener { task ->
if (!task.isSuccessful) {
// Handle error
// ...
}
val invite = FirebaseAppInvite.getInvitation(task.result)
if (invite != null) {
// Handle invite
// ...
}
}
// [END ddl_get_invitation]
}
fun onboardingShare(dl: ShortDynamicLink) {
// [START ddl_onboarding_share]
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Try this amazing app: ${dl.shortLink}")
}
startActivity(Intent.createChooser(intent, "Share using"))
// [END ddl_onboarding_share]
}
fun buildDeepLink(deepLink: Uri, minVersion: Int): Uri {
val uriPrefix = "https://YOUR_APP.page.link"
// Set dynamic link parameters:
// * URI prefix (required)
// * Android Parameters (required)
// * Deep link
// [START build_dynamic_link]
// Build the dynamic link
val link = Firebase.dynamicLinks.dynamicLink {
domainUriPrefix = uriPrefix
androidParameters {
minimumVersion = minVersion
}
link = deepLink
}
// [END build_dynamic_link]
// Return the dynamic link as a URI
return link.uri
}
}