-
Notifications
You must be signed in to change notification settings - Fork 398
/
MainActivity.java
193 lines (177 loc) · 8.27 KB
/
MainActivity.java
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
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.quickstart.dynamiclinks;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.appinvite.AppInviteReferral;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.appinvite.FirebaseAppInvite;
import com.google.firebase.dynamiclinks.DynamicLink;
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks;
import com.google.firebase.dynamiclinks.PendingDynamicLinkData;
import com.google.firebase.dynamiclinks.ShortDynamicLink;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createDynamicLink_Basic() {
// [START create_link_basic]
DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.example.com/"))
.setDomainUriPrefix("https://example.page.link")
// Open links with this app on Android
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
// Open links with com.example.ios on iOS
.setIosParameters(new DynamicLink.IosParameters.Builder("com.example.ios").build())
.buildDynamicLink();
Uri dynamicLinkUri = dynamicLink.getUri();
// [END create_link_basic]
}
public void createDynamicLink_Advanced() {
// [START create_link_advanced]
DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.example.com/"))
.setDomainUriPrefix("https://example.page.link")
.setAndroidParameters(
new DynamicLink.AndroidParameters.Builder("com.example.android")
.setMinimumVersion(125)
.build())
.setIosParameters(
new DynamicLink.IosParameters.Builder("com.example.ios")
.setAppStoreId("123456789")
.setMinimumVersion("1.0.1")
.build())
.setGoogleAnalyticsParameters(
new DynamicLink.GoogleAnalyticsParameters.Builder()
.setSource("orkut")
.setMedium("social")
.setCampaign("example-promo")
.build())
.setItunesConnectAnalyticsParameters(
new DynamicLink.ItunesConnectAnalyticsParameters.Builder()
.setProviderToken("123456")
.setCampaignToken("example-promo")
.build())
.setSocialMetaTagParameters(
new DynamicLink.SocialMetaTagParameters.Builder()
.setTitle("Example of a Dynamic Link")
.setDescription("This link works whether the app is installed or not!")
.build())
.buildDynamicLink(); // Or buildShortDynamicLink()
// [END create_link_advanced]
}
public void createShortLink() {
// [START create_short_link]
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.example.com/"))
.setDomainUriPrefix("https://example.page.link")
// Set parameters
// ...
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
} else {
// Error
// ...
}
}
});
// [END create_short_link]
}
public void shortenLongLink() {
// [START shorten_long_link]
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLongLink(Uri.parse("https://example.page.link/?link=https://www.example.com/&apn=com.example.android&ibn=com.example.ios"))
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
@Override
public void onComplete(@NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
} else {
// Error
// ...
}
}
});
// [END shorten_long_link]
}
public void buildShortSuffix() {
// [START ddl_short_suffix]
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
// ...
.buildShortDynamicLink(ShortDynamicLink.Suffix.SHORT);
// ...
// [END ddl_short_suffix]
}
public void shareLink(Uri myDynamicLink) {
// [START ddl_share_link]
Intent sendIntent = new Intent();
String msg = "Hey, check this out: " + myDynamicLink;
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, msg);
sendIntent.setType("text/plain");
startActivity(sendIntent);
// [END ddl_share_link]
}
public void getInvitation() {
// [START ddl_get_invitation]
FirebaseDynamicLinks.getInstance()
.getDynamicLink(getIntent())
.addOnCompleteListener(new OnCompleteListener<PendingDynamicLinkData>() {
@Override
public void onComplete(@NonNull Task<PendingDynamicLinkData> task) {
if (!task.isSuccessful()) {
// Handle error
// ...
}
FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(task.getResult());
if (invite != null) {
// Handle invite
// ...
}
}
});
// [END ddl_get_invitation]
}
public void getDeepLink() {
Intent intent = getIntent();
// [START ddl_get_deep_link]
String link = AppInviteReferral.getDeepLink(intent);
// [END ddl_get_deep_link]
}
public void onboardingShare(ShortDynamicLink dl) {
// [START ddl_onboarding_share]
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Try this amazing app: " + dl.getShortLink());
startActivity(Intent.createChooser(intent, "Share using"));
// [END ddl_onboarding_share]
}
}