Facebook App Links and how to transfer data between two android apps with Messenger message

In sample words Facebook gives us the opportunity  to invite users to our website or mobile app by sending invitations in the Facebook Messenger app.

More information can be found here: https://developers.facebook.com/docs/applinks/android.

So I will share the example I am trying so you can understand what the idea is.

I am writing a picture sharing app. A user can share a picture by sending a special link to a friend in facebook messenger. When his friend clicks on the link: if (he has the app) ? the app will open the picture else: he will be redirected to the apps page in google play and after he installs it on second click of the messenger link the app will display the picture. I am also using ParseFacebookUtils from Parse.Com to login the user in facebook.

To do all the steps below the best thing you could do is to have a SERVER. We did it without one risking our own application by putting application data in the mobile app.

To do this there are few things I need to do:

  1. Download facebook sdk for android
  2. Decide what information I want to share with the other user using facebook sdk
  3. Provide that information which I want to share and the information about the mobile app (link to Google Play and app name) to facebook
  4. Facebook will give me a unique link which I will include in the message to the user
  5. Insert the link in the Facebook Messenger message and send an intent to the Messenger so the user can pick to which friends he wants to send the message
  6. On click of the message – that link is opened by facebook and based on the info that they have they decide to open the app or redirect the user to the Google Play Store

To do these steps:

  • You need to have you apps token – this is what the token looks like: <app_id>|<app_secret> . Example: 1504634053123451|41072f7b872ce14bf7ca8315f85ace14
  • You need to setup the data you have for your app. If you have also an iOS version you can add iOS data so if the message is sent to a user with iOS facebook can redirect him to the iTunes to download the app. User can also be redirected to a custom website. That is given in the web: shouldFallback param.
Bundle params = new Bundle();
params.putString("name", "Card Link Name");
params.putString("android", "[{"app_name": ""+getString(R.string.app_name)+"" ," +
		""class": ""," + 
		""package":"com.iovchev.christmascards"," +
		""url":"christmascards://postcard?id="+cardId+""}]"
		);

params.putString("ios", "[{"app_name": "Christmas Gift Camera: Free Xmas Live Photo Frames and Effects. Share Personal Voice Present Cards with Friends and Family."," +
			""app_store_id": 942404230," +
			""url": "params://postcard"}]");
params.putString("web", "{"should_fallback": false}");
params.putString("access_token", accessToken);
  •  After you fill all the needed data you need to make a request to the Facebook API so it can generate you a special id for that info.
  • With that id you make a second request to get the special url which you can share with the users in messenger which looks like: http://fb.me/10231321321312. This is accomplished in the code below:

VERY IMPORTANT: For Android:  The data you want to share should be passed through the url option in the android key name. As you can see I pass the card ID there. If you want your app to be called with the url you are giving then leave the class field blank. If you give it FB will call you app with an empty intent and you will never receive the url data.

new Request(
    ParseFacebookUtils.getSession(),
    "/app/app_link_hosts",
    params,
    HttpMethod.POST,
    new Request.Callback() {
	    public void onCompleted(Response response) {
		    Log.d("RESPONSE", response.toString());
		    String id = "";
		    try {
			    id = response.getGraphObject().getInnerJSONObject().getString("id");
		    } catch (JSONException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
		    }
		    Log.d("RESULT", id + "  id");
		    Bundle params2 = new Bundle();
		    params2.putString("access_token", accessToken);
		    new Request(
				    ParseFacebookUtils.getSession(),
				    "/" + id,
				    params2,
				    HttpMethod.GET,
				    new Request.Callback() {
					    public void onCompleted(Response response) {
						    String url = "";
						    try {
							    url = response.getGraphObject().getInnerJSONObject().getString("canonical_url");
						    } catch (JSONException e) {
							    // TODO Auto-generated catch block
							    e.printStackTrace();
						    }
						    Log.d("RESPONSE", response.toString());
						    FacebookDialog.MessageDialogBuilder builder = 
								    new FacebookDialog.MessageDialogBuilder(SendCardActivity.this)
						    .setLink(url)
						    .setName("Test title")
						    .setCaption("Build great social apps that engage your friends.")
						    .setPicture("http://i.imgur.com/g3Qc1HN.png")
						    .setDescription("Allow your users to message links from your app using the Android SDK.");
						    FacebookDialog dialog = builder.build();
						    dialog.present();
					    }
				    }
				    ).executeAsync();
	    }	
    }
    ).executeAsync();

You may also like...