11package com .google .firebase .example .invites ;
22
33import android .content .Intent ;
4+ import android .net .Uri ;
5+ import android .os .Bundle ;
6+ import android .support .annotation .NonNull ;
7+ import android .support .design .widget .Snackbar ;
48import android .support .v7 .app .AppCompatActivity ;
9+ import android .util .Log ;
10+ import android .view .View ;
11+ import android .view .ViewGroup ;
512
613import com .google .android .gms .appinvite .AppInviteInvitation ;
14+ import com .google .android .gms .tasks .OnFailureListener ;
15+ import com .google .android .gms .tasks .OnSuccessListener ;
16+ import com .google .firebase .appinvite .FirebaseAppInvite ;
17+ import com .google .firebase .dynamiclinks .FirebaseDynamicLinks ;
18+ import com .google .firebase .dynamiclinks .PendingDynamicLinkData ;
719
820import devrel .firebase .google .com .firebaseoptions .R ;
921
10- public class MainActivity extends AppCompatActivity {
22+ /**
23+ * App Invites is deprecated, this file serves only to contain snippets that are still
24+ * referenced in some documentation.
25+ */
26+ public class MainActivity extends AppCompatActivity implements View .OnClickListener {
27+
28+ private static final String TAG = "MainActivity" ;
1129
1230 private static final String IOS_APP_CLIENT_ID = "foo-bar-baz" ;
1331 private static final int REQUEST_INVITE = 101 ;
1432
33+ // [START on_create]
34+ @ Override
35+ protected void onCreate (Bundle savedInstanceState ) {
36+ // [START_EXCLUDE]
37+ super .onCreate (savedInstanceState );
38+ setContentView (R .layout .activity_main );
39+
40+ // Invite button click listener
41+ findViewById (R .id .inviteButton ).setOnClickListener (this );
42+ // [END_EXCLUDE]
43+
44+ // Check for App Invite invitations and launch deep-link activity if possible.
45+ // Requires that an Activity is registered in AndroidManifest.xml to handle
46+ // deep-link URLs.
47+ FirebaseDynamicLinks .getInstance ().getDynamicLink (getIntent ())
48+ .addOnSuccessListener (this , new OnSuccessListener <PendingDynamicLinkData >() {
49+ @ Override
50+ public void onSuccess (PendingDynamicLinkData data ) {
51+ if (data == null ) {
52+ Log .d (TAG , "getInvitation: no data" );
53+ return ;
54+ }
55+
56+ // Get the deep link
57+ Uri deepLink = data .getLink ();
58+
59+ // Extract invite
60+ FirebaseAppInvite invite = FirebaseAppInvite .getInvitation (data );
61+ if (invite != null ) {
62+ String invitationId = invite .getInvitationId ();
63+ }
64+
65+ // Handle the deep link
66+ // [START_EXCLUDE]
67+ Log .d (TAG , "deepLink:" + deepLink );
68+ if (deepLink != null ) {
69+ Intent intent = new Intent (Intent .ACTION_VIEW );
70+ intent .setPackage (getPackageName ());
71+ intent .setData (deepLink );
72+
73+ startActivity (intent );
74+ }
75+ // [END_EXCLUDE]
76+ }
77+ })
78+ .addOnFailureListener (this , new OnFailureListener () {
79+ @ Override
80+ public void onFailure (@ NonNull Exception e ) {
81+ Log .w (TAG , "getDynamicLink:onFailure" , e );
82+ }
83+ });
84+ }
85+ // [END on_create]
86+
87+ /**
88+ * User has clicked the 'Invite' button, launch the invitation UI with the proper
89+ * title, message, and deep link
90+ */
91+ // [START on_invite_clicked]
92+ private void onInviteClicked () {
93+ Intent intent = new AppInviteInvitation .IntentBuilder (getString (R .string .invitation_title ))
94+ .setMessage (getString (R .string .invitation_message ))
95+ .setDeepLink (Uri .parse (getString (R .string .invitation_deep_link )))
96+ .setCustomImage (Uri .parse (getString (R .string .invitation_custom_image )))
97+ .setCallToActionText (getString (R .string .invitation_cta ))
98+ .build ();
99+ startActivityForResult (intent , REQUEST_INVITE );
100+ }
101+ // [END on_invite_clicked]
102+
103+ // [START on_activity_result]
104+ @ Override
105+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
106+ super .onActivityResult (requestCode , resultCode , data );
107+ Log .d (TAG , "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode );
108+
109+ if (requestCode == REQUEST_INVITE ) {
110+ if (resultCode == RESULT_OK ) {
111+ // Get the invitation IDs of all sent messages
112+ String [] ids = AppInviteInvitation .getInvitationIds (resultCode , data );
113+ for (String id : ids ) {
114+ Log .d (TAG , "onActivityResult: sent invitation " + id );
115+ }
116+ } else {
117+ // Sending failed or it was canceled, show failure message to the user
118+ // [START_EXCLUDE]
119+ showMessage (getString (R .string .send_failed ));
120+ // [END_EXCLUDE]
121+ }
122+ }
123+ }
124+ // [END on_activity_result]
125+
15126 public void sendInvitationIOS () {
16127 // [START invites_send_invitation_ios]
17128 Intent intent = new AppInviteInvitation .IntentBuilder (getString (R .string .invitation_title ))
@@ -24,4 +135,16 @@ public void sendInvitationIOS() {
24135 // [END invites_send_invitation_ios]
25136 }
26137
138+ private void showMessage (String msg ) {
139+ ViewGroup container = findViewById (R .id .snackbarLayout );
140+ Snackbar .make (container , msg , Snackbar .LENGTH_SHORT ).show ();
141+ }
142+
143+ @ Override
144+ public void onClick (View view ) {
145+ int i = view .getId ();
146+ if (i == R .id .inviteButton ) {
147+ onInviteClicked ();
148+ }
149+ }
27150}
0 commit comments