/
MainActivity.java
150 lines (131 loc) · 5.8 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
package com.google.firebase.example.invites;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.appinvite.AppInviteInvitation;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.appinvite.FirebaseAppInvite;
import com.google.firebase.dynamiclinks.FirebaseDynamicLinks;
import com.google.firebase.dynamiclinks.PendingDynamicLinkData;
import devrel.firebase.google.com.firebaseoptions.R;
/**
* App Invites is deprecated, this file serves only to contain snippets that are still
* referenced in some documentation.
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private static final String IOS_APP_CLIENT_ID = "foo-bar-baz";
private static final int REQUEST_INVITE = 101;
// [START on_create]
@Override
protected void onCreate(Bundle savedInstanceState) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Invite button click listener
findViewById(R.id.inviteButton).setOnClickListener(this);
// [END_EXCLUDE]
// Check for App Invite invitations and launch deep-link activity if possible.
// Requires that an Activity is registered in AndroidManifest.xml to handle
// deep-link URLs.
FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
.addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
@Override
public void onSuccess(PendingDynamicLinkData data) {
if (data == null) {
Log.d(TAG, "getInvitation: no data");
return;
}
// Get the deep link
Uri deepLink = data.getLink();
// Extract invite
FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(data);
if (invite != null) {
String invitationId = invite.getInvitationId();
}
// Handle the deep link
// [START_EXCLUDE]
Log.d(TAG, "deepLink:" + deepLink);
if (deepLink != null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(getPackageName());
intent.setData(deepLink);
startActivity(intent);
}
// [END_EXCLUDE]
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "getDynamicLink:onFailure", e);
}
});
}
// [END on_create]
/**
* User has clicked the 'Invite' button, launch the invitation UI with the proper
* title, message, and deep link
*/
// [START on_invite_clicked]
private void onInviteClicked() {
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
.setCallToActionText(getString(R.string.invitation_cta))
.build();
startActivityForResult(intent, REQUEST_INVITE);
}
// [END on_invite_clicked]
// [START on_activity_result]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
if (requestCode == REQUEST_INVITE) {
if (resultCode == RESULT_OK) {
// Get the invitation IDs of all sent messages
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
for (String id : ids) {
Log.d(TAG, "onActivityResult: sent invitation " + id);
}
} else {
// Sending failed or it was canceled, show failure message to the user
// [START_EXCLUDE]
showMessage(getString(R.string.send_failed));
// [END_EXCLUDE]
}
}
}
// [END on_activity_result]
public void sendInvitationIOS() {
// [START invites_send_invitation_ios]
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
// ...
.setOtherPlatformsTargetApplication(
AppInviteInvitation.IntentBuilder.PlatformMode.PROJECT_PLATFORM_IOS,
IOS_APP_CLIENT_ID)
// ...
.build();
// [END invites_send_invitation_ios]
}
private void showMessage(String msg) {
ViewGroup container = findViewById(R.id.snackbarLayout);
Snackbar.make(container, msg, Snackbar.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.inviteButton) {
onInviteClicked();
}
}
}