Tuesday 10 November 2015

Facebook App Deep Linking - Andoid

One of the most interesting aspects of sharing to Facebook from your app is that when people engage with the feed stories posted from your app, those stories can send people to your app or your app's Google Play listing, driving traffic and app installs. You can implement this behavior using App Links.

   App link Flow


Facebook's Mobile Hosting API for App Links:


If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links.

For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you. With the Hosting API you can create and manage your App Links for all the mobile environments you support.

This is the typical flow to configure an App Link for a piece of content:
  1. Create a new App Link object. You can set up an object for one platform or several at the same time.
  2. Save the App Link object ID that's returned.
  3. Use that ID to get the URL you can use to share the content.
  4. Configure additional platforms that you support.
  5. If supporting Android, configure your app's manifest file for the configured URLs.

Please follow below steps for generate app linking.


For web service, In request Params we need to send below parameters.

iOS: url, name
Android: url, name

Step #1: Generate App Access Token (Ref. https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens)

GET /oauth/access_token?     client_id={app-id}    &client_secret={app-secret}    &grant_type=client_credentials


Step #2: Generate App Link using generated App Access Token (Ref. https://developers.facebook.com/docs/applinks/hosting-api)


You create a new App Link object for a content targeted to iOS & Android  like this

curl https://graph.facebook.com/app/app_link_hosts \
-F access_token="APP_ACCESS_TOKEN" \
-F name="iOS App Link Object Example" \
-F ios=' [
    {
      "url" : "applinkFB://showApp/54eee6292658c7df25000004",
      "app_store_id" : 570281083,
      "app_name" : “appName”,
    },
  ]' \
-F android=' [
    {
      "url" : "applinkFB://showApp/54eee6292658c7df25000004",
      "package" : "com.packagename”,
      "app_name" : “appName”,
    },
  ]' \

-F web=' {
    "should_fallback" : false,
  }'


A successful call returns an ID that represents the App Link object hosted on Facebook,
for example:

Return : {"id":"643402985734254"}

Step #3: Using generated ID(YOUR_APP_LINK_HOST_ID) which represents the App Link, you need to call another API which will return "canonical_url"


You can retrieve the canonical URL that points to your new App Link object through this API call:

curl -G https://graph.facebook.com/YOUR_APP_LINK_HOST_ID \
-d access_token="APP_ACCESS_TOKEN" \
-d fields=canonical_url \
-d pretty=true

Where YOUR_APP_LINK_HOST_ID represents the id returned from creating your App Link object. Your response will look like this:

Return:
{
   "canonical_url": "https://fb.me/643402985734254",   "id": "643402985734254"
}


Support sharesample URLs in Android

When a shared link is tapped your Android app is launched through the URL specified in the App Link object you just created (if you didn't specify a url, then it will use the canonical URL - i.e. the https://fb.me/xxxxx URL). To set up your app to respond to this URL, you need to add an intent filter in your app's manifest file.

The filter should respond to the applinkFB scheme (if you didn't specify a URL, then your filter should respond to the fb.me host and https scheme), handle implicit intents, and accept the ACTION_VIEW action. The example below adds an intent filter to the MainActivity that handles this:



    ...
    
        
        
        
        
    


Once you've set up your web pages based App Links, 
you're ready to handle incoming links to your app.

Handling incoming links

To ensure an engaging user experience, you should process the incoming intent information when your app is activated and direct the person to the object featured in the story they're coming from. 

For example, if I see a story on my Facebook feed about one of my friends completing this share tutorial and I tap on it, I will expect to be redirected to a view in your app that features this tutorial and not to your app's main activity.

If you use the App Links Hosting API, the Intent data will look like this:


data: "applinkFB://showApp/54eee6292658c7df25000004?target_url=https%3A%2F%2Ffb.me%2F643402985734299"
extras:
  al_applink_data: <Bundle containing App Link data>

In the code sample below, we're simply logging the target URL, but you should direct people through the appropriated flow for your app:


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(this); 
    ...
    Uri targetUrl =
      AppLinks.getTargetUrlFromInboundIntent(this, getIntent());
    if (targetUrl != null) {
        Log.i("Activity", "App Link Target URL: " + targetUrl.toString());
    }
}

Wednesday 4 November 2015

Twitter- Deep Linking with Android

Twitter - Deep Linking for Mobile Developers

The App Card provides the ability for users to download your app (if the user doesn’t already have it installed), or deep-link into your own app (if the app is already installed on the user’s mobile device).
We can achieve this things with Twitter Card.

https://dev.twitter.com/cards/mobile

We need to make dynamic Web page and host this page on any server which will be use for Sharing using Twitter from app. 

For demonstration purpose, we have implemented the same functionality in PHP with some hard-coded data but you need to convert in your back-end technology with dynamic data.

Please find PHP source code which we used for testing purpose.

Web Page Part
<html>
<head> 
<title>App Name</title>
<meta name="twitter:card" content="summary">

<meta name="twitter:site" content="App Name">
<meta name="twitter:title" content="App Name">

<meta name="twitter:description" content="App Name is a 100% free social sharing app that allows sharing of video, photo, audio and text all in one network.">
<meta name="twitter:image" content="http://app_name.vm39.sa92.info/ico.png">
<meta name="twitter:url" content="http://appname.com/">
<meta name="twitter:app:name:iphone" content="appname"/>
<meta name="twitter:app:id:iphone" content="570281083"/>
<meta name="twitter:app:name:googleplay" content="appname"/>
<meta name="twitter:app:id:googleplay" content="com.appname"/>
<meta name="twitter:app:url:iphone" content="appname://show?bid=54e58b4360afd71a53001f5e" />
<meta name="twitter:app:url:googleplay" content="appname://show?bid=54e58b4360afd71a53001f5e" />
</head>
<body>
<h1>Have something interesting here for particular Post, becasue when user click on link from Twitter it he/she will come to this page.</h1>
</body>
</html>

We have used summary card here.
https://dev.twitter.com/cards/types/summary
https://dev.twitter.com/cards/types/app

After making card, you have to validate your card with below URL.
https://cards-dev.twitter.com/validator

Android Part

At Android side, we need to do below things.
We need to add host and scheme in activity tag of Manifest file based on url defined in twitter app link url above.

Please find below code snippets below for reference.

AndroidManifest.xml

 <activity

            android:name=".ui.activities.SplashActivity"

            android:label="@string/app_name"

            android:launchMode="singleTop"

            android:screenOrientation="sensorPortrait"

            android:windowSoftInputMode="adjustResize" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

            <intent-filter android:label="@string/app_name" >

                <action android:name="android.intent.action.VIEW" >
                </action>
                <category android:name="android.intent.category.DEFAULT" >
                </category>
                <category android:name="android.intent.category.BROWSABLE" >
                </category>


                <!-- Accepts URIs that begin with "example://action" -->

                <data

                    android:host="show"
                    android:scheme="appname" >

                </data>

            </intent-filter>
        </activity> 



In Activity, You can retrieve app link url in oncreate() method.
With url, you can redirect user to our app's screen based on data retrieved from URL.

Please find below code snippets below for reference.

onCreate() 
if (getIntent().getData() != null

                && getIntent().getData().toString().length() > 0) {


            Logs.e(TAG, "Data:" + getIntent().getData());

        if (getIntent().getData().toString().startsWith("appname")) {


                twitterCallbackUrl = getIntent().getData().toString()
                        .substring(getIntent().getData().toString().lastIndexOf("?bid=") + 5,
                                getIntent().getData().toString().length());



                Logs.e(TAG, "twitterCallbackUrl:" + twitterCallbackUrl);
            }
        }