Firebase App indexing API 本地使用

Firebase App indexing API 本地使用

相关链接

  1. firebase官方文档
  2. developer官网资料
  3. developer官网training

集成步骤

1. 配置google-services插件,firebase app indexing API使用需要基于google services

项目build.gradle:

    dependencies {
        classpath 'com.google.gms:google-services:4.0.0'

    }

app/build.gradle最后添加:

apply plugin: 'com.google.gms.google-services'

app/build.gradle中添加库依赖:

dependencies {
    compile 'com.google.firebase:firebase-appindexing:16.0.1'
}

另外,需要通过注册firebase应用,获取到google-services.json,不然编译不过,google-services插件依赖此json文件

2. AndroidManifests.xml中添加deep link支持

        <activity
            android:name=".client.RecipeActivity"
            android:exported="true"
            android:launchMode="singleTop">
            <intent-filter android:label="@string/app_name" android:autoVerify="true">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <!--<category android:name="android.intent.category.BROWSABLE" />-->
                <!-- Accepts URIs that begin with "http://recipe-app.com/recipe" -->
                <data android:scheme="http" />
                <data android:scheme="https" />
                <!--<data android:scheme="http"
                    android:host="appindexing.com"
                    android:pathPrefix="/test" />-->
                <!--<data android:scheme="http"
                    android:host="recipe-app.com"
                    android:pathPrefix="/recipe" />-->
            </intent-filter>
        </activity>

3. 在代码中通过app indexing API添加索引,便于Google Search关联到索引

        noteToIndex = new Indexable.Builder()
                .setName("Facebook")
                .setUrl("https://facebook.com")
                .setImage("content://com.app.indexing.demo.imgprovider/icon/app_icon.png")
//                .setImage("http://liberty-vaults-storage.s3.amazonaws.com/services/icons/11567/original/mynuwaveoven.png?1533806001")
                .build();
        indexables.add(noteToIndex);

        Indexable[] arr = indexables.toArray(new Indexable[indexables.size()]);

        Task<Void> task = FirebaseAppIndex.getInstance().update(arr);
        // [START_EXCLUDE]
        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.d(TAG, "App Indexing API: Successfully added note to index");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Log.e(TAG, "App Indexing API: Failed to add note to index. " + exception
                        .getMessage());
            }
        });

4. google search中默认显示的icon是应用icon,可以使用网络图片或者本地图片进行修改

设置本地图片,通过provider提供访问:

    @Override
    public AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {

        if (uri == null) {
            return null;
        }
        AssetFileDescriptor pfd= null;
        try {
            pfd = getContext().getAssets().openFd("app_icon.png");
        } catch (IOException e) {
        }
        return pfd;
    }

猜你喜欢

转载自blog.csdn.net/qinhai1989/article/details/82146905