java - 从应用程序邀请中获取推荐历史的选项?

标签 java android deep-linking referrals google-app-invites

我尝试通过邮件和短信邀请应用程序并成功做到了。但我需要获取邀请 friend 历史的推荐历史,即推荐 friend 姓名和邮件 ID。我通过谷歌搜索并尝试过,只有机会获得邀请 ID。是否有任何其他选项可以使用 google API app invite 获取推荐 friend 的历史详细信息。我在这里提交了代码。如下请见。谢谢。

主要 Activity


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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.AppInvite;
import com.google.android.gms.appinvite.AppInviteInvitation;
import com.google.android.gms.appinvite.AppInviteInvitationResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;

/**
 * Main Activity for sending App Invites and launchings the DeepLinkActivity when an
 * App Invite is received.
 */
public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int REQUEST_INVITE = 0;

    private GoogleApiClient mGoogleApiClient;

    // [START on_create]
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // [START_EXCLUDE]
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
// No savedInstanceState, so it is the first launch of this activity


        // Invite button click listener
        findViewById(R.id.invite_button).setOnClickListener(this);
        findViewById(R.id.custom_invite_button).setOnClickListener(this);
        // [END_EXCLUDE]

        // Create an auto-managed GoogleApiClient with acccess to App Invites.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(AppInvite.API)
                .enableAutoManage(this, this)
                .build();

        // 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.
        boolean autoLaunchDeepLink = true;
        AppInvite.AppInviteApi.getInvitation(mGoogleApiClient, this, autoLaunchDeepLink)
                .setResultCallback(
                        new ResultCallback<AppInviteInvitationResult>() {
                            @Override
                            public void onResult(AppInviteInvitationResult result) {
                                Log.d(TAG, "getInvitation:onResult:" + result.getStatus());
                                Log.e(TAG, "Redult:---->" + result.toString());
                                // Because autoLaunchDeepLink = true we don't have to do anything
                                // here, but we could set that to false and manually choose
                                // an Activity to launch to handle the deep link here.
                            }
                        });
    }
    /* [END on_create] */

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.d(TAG, "onConnectionFailed:" + connectionResult);
        showMessage(getString(R.string.google_play_services_error));
    }

    /**
     * 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]

    /**
     * User has clicked the 'Custom Invite' button, launch the invitation UI but pass in
     * a custom HTML body and subject for email invites.
     */
    // [START on_custom_invite_clicked]
    private void onCustomInviteClicked() {
        // When using the setEmailHtmlContent method, you must also set a subject using the
        // setEmailSubject message and you may not use either setCustomImage or setCallToActionText
        // in conjunction with the setEmailHtmlContent method.
        //
        // The "%%APPINVITE_LINK_PLACEHOLDER%%" token is replaced by the invitation server
        // with the custom invitation deep link based on the other parameters you provide.
        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)))
                .setEmailHtmlContent("<html><body>" +
                        "<h1>App Invites</h1>" +
                        "<a href=\"%%APPINVITE_LINK_PLACEHOLDER%%\">Install Now!</a>" +
                        "<body></html>")
                .setEmailSubject(getString(R.string.invitation_subject))
                .build();
        startActivityForResult(intent, REQUEST_INVITE);

     /*   Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + "9786228072"));
        intent.putExtra("sms_body", "install");*/
       // startActivity(intent);
    }
    // [END on_custom_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);
        Log.e("onActivityResult", "onActivityResult" + requestCode + " :  " + resultCode);
        if (requestCode == REQUEST_INVITE) {
            if (resultCode == RESULT_OK) {
                // Check how many invitations were sent and log a message
                // The ids array contains the unique invitation ids for each invitation sent
                // (one for each contact select by the user). You can use these for analytics
                // as the ID will be consistent on the sending and receiving devices.
                String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
                Log.d(TAG, getString(R.string.sent_invitations_fmt, ids.length));
                Log.e("sent_invitations_fmt","sent_invitations_fmt"+ ids.length);
            } else {
                // Sending failed or it was canceled, show failure message to the user
                showMessage(getString(R.string.send_failed));
            }
        }
    }
    // [END on_activity_result]

    private void showMessage(String msg) {
        ViewGroup container = (ViewGroup) findViewById(R.id.snackbar_layout);
        Snackbar.make(container, msg, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.invite_button:
                onInviteClicked();
                break;
            case R.id.custom_invite_button:
                onCustomInviteClicked();
                break;
        }
    }
}

DeeplinkActivity:


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.appinvite.AppInviteReferral;


/**
 * Activity for displaying information about a receive App Invite invitation.  This activity
 * displays as a Dialog over the MainActivity and does not cover the full screen.
 */


public class DeepLinkActivity extends AppCompatActivity implements
        View.OnClickListener {

    private static final String TAG = DeepLinkActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deep_link_activity);
        Log.e("deepclass", "deepclass: ");
        // Button click listener
        findViewById(R.id.button_ok).setOnClickListener(this);
    }

    // [START deep_link_on_start]
    @Override
    protected void onStart() {
        super.onStart();
        // Check if the intent contains an AppInvite and then process the referral information.
        Intent intent = getIntent();
        if (AppInviteReferral.hasReferral(intent)) {
            processReferralIntent(intent);
        }
    }

    // [END deep_link_on_start]
    // [START process_referral_intent]
    private void processReferralIntent(Intent intent) {
        // Extract referral information from the intent
        String invitationId = AppInviteReferral.getInvitationId(intent);

        String deepLink = AppInviteReferral.getDeepLink(intent);


        if (AppInviteReferral.isOpenedFromPlayStore(intent)) {
            Log.e("invite","invite");
        }else{
            Log.e("not invite","not invite");
        }

        addReferralDataToIntent(invitationId, deepLink, intent);
        // Display referral information
        // [START_EXCLUDE]
        Log.d(TAG, "Found Referral: " + invitationId + ":" + deepLink);
        Log.e("deepLink", "deepLink: " + invitationId + ":" + deepLink);
        ((TextView) findViewById(R.id.deep_link_text))
                .setText(getString(R.string.deep_link_fmt, deepLink));
        ((TextView) findViewById(R.id.invitation_id_text))
                .setText(getString(R.string.invitation_id_fmt, invitationId));
        // [END_EXCLUDE]
    }

    public static Intent addReferralDataToIntent (String invitationId, String deepLink, Intent referralIntent){

        Log.e("deepLink", "deepLink: " + deepLink);
        Log.e("invitationId", "invitationId: " + invitationId);

        return referralIntent;``

    }
    // [END process_referral_intent]

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_ok:
                finish();
                break;
        }
    }
}

最佳答案

您可以在构建深层链接时将此信息添加到深层链接中:

Intent intent = new AppInviteInvitation.IntentBuilder("Invitation Title")
        .setDeepLink(Uri.parse("http://sample/refmail" + mailAddr + "/refName/" + name ))

您的 Activity Intent 过滤器已在架构“http”和主机“sample”上注册,但您从 AppInviteReferral 获得的深度链接是整个字符串,因此您可以从中解析请求的信息。

关于java - 从应用程序邀请中获取推荐历史的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35839575/

相关文章:

java - 实现不同存储策略的模式

android - 如何使用 NDK 通过 Android 配置脚本构建开源程序?

ios - iOS 中的通用链接(Facebook、Twitter、LinkedIn)

java - 和engine BitmapTextureAtlas

android - WebView 中的 IndexOutOfBoundsException

android - 是否可以在 Android 上使用 URL(深层链接)打开设置应用程序?

ios - 我如何通过通用链接使用深度链接打开同一个应用程序

java - 无法让 AdMob 广告显示在 libgdx 游戏中

java - 来自HttpSessionListener的sessionCreated()是否自动与request.getSession()同步?

java - 抽象工厂设计模式