简体   繁体   中英

PendingDynamicLinkData is null

I am developing an app which can share and been reward so I use the concept of the dynamic link so to store the information of inviter in that link and later be rewarded. but the PendingDynamicLinkData return null on installing the app after referred.

MainActivity

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    googleSignInClient = GoogleSignIn.getClient(this, gso);

    checkInvitations();
}

void checkInvitations() {

    FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData data) {
                    if (data == null) {
                        Diagnostics.e(this, "No data").append(activity);
                        return;
                    }

                    // Get the deep link
                    Uri deepLink = data.getLink();

                    // Extract invite
                    FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(data);
                    if (invite != null) {
                        String invitationId = invite.getInvitationId();
                        Diagnostics.i(this, "Send invitationId to server. InvitationId = " + invitationId).append(activity);
                    }

                    Diagnostics.i(this, "deepLink:" + deepLink).append(activity);

                    if (deepLink != null) {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setPackage(getPackageName());
                        intent.setData(deepLink);

                        startActivity(intent);
                    }
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "getDynamicLink:onFailure", e);
                }
            });
}

onClick event on Invite button in MainActivity

void onInviteClicked() {

    JSONObject json = jsonObjectCreator.createJSONObjectForSendingUserId(getUserId());
    String jsonString = json.toString();

    getterReferenceId = new GetFromServer(this, GET_REFERENCE_URL, jsonString);
    getterReferenceId.execute();
}

GetFromServer class

static class GetFromServer extends AsyncTask<Void, Void, JSONObject> {

    private String mUrl;
    private String mSpotsReportJson;
    private WeakReference<MainActivity> activityReference;

    GetFromServer(MainActivity context, String url, String spotsReportJson) {
        activityReference = new WeakReference<>(context);
        mUrl = url;
        mSpotsReportJson = spotsReportJson;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        MainActivity activity = activityReference.get();
        if (activity == null || activity.isFinishing()) return;
    }

    @Override
    protected JSONObject doInBackground(Void... voids) {

        StringBuilder data = new StringBuilder();
        HttpURLConnection httpURLConnection = null;
        MainActivity activity = activityReference.get();

        // get JSON
        try {
            httpURLConnection = (HttpURLConnection) new URL(mUrl).openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.setDoOutput(true);

            if (isCancelled()) return null;

            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(mSpotsReportJson);
            wr.flush();
            wr.close();

            int response = httpURLConnection.getResponseCode();

            if (response == HttpURLConnection.HTTP_OK || response == HttpURLConnection.HTTP_CREATED) {
                // Success
                InputStream in = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(in);

                int inputStreamData = inputStreamReader.read();
                while (inputStreamData != -1) {
                    char current = (char) inputStreamData;
                    inputStreamData = inputStreamReader.read();
                    data.append(current);
                }
            }
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionDetails = sw.toString();
            Diagnostics.e(this, "Exception caught: " + exceptionDetails).append(activity);
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }

        try {
            JSONObject json = new JSONObject(data.toString());
            return json;
        } catch (JSONException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionDetails = sw.toString();
            Diagnostics.e(this, "Exception caught: " + exceptionDetails).append(activity);
        }
        return null;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);

                String referenceId = getReferenceIdFromJson(json);

                if (referenceId != null) {
                    // create dynamic link
                    String link = "https://www.hu.info/_invites/" + referenceId;
                    // create Firebase link
                        FirebaseDynamicLinks.getInstance().createDynamicLink()
                                .setLink(Uri.parse(link))
                                .setDomainUriPrefix("https://hu.page.link")
                                .setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
                                .buildShortDynamicLink()
                                .addOnCompleteListener(activity, new OnCompleteListener<ShortDynamicLink>() {
                                    @Override
                                    public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                                        if (task.isSuccessful()) {
                                            // Short link created
                                            ShortDynamicLink shortDynamicLink = task.getResult();
                                            if (shortDynamicLink != null) {
                                                Uri shortLink = shortDynamicLink.getShortLink();

                                                // send reference
                                                Intent intent = new AppInviteInvitation.IntentBuilder(activity.getString(R.string.invitation_title))
                                                        .setMessage(activity.getString(R.string.invitation_message))
                                                        .setDeepLink(shortLink)
                                                        .setCallToActionText(activity.getString(R.string.invitation_cta))
                                                        .build();                                                            activity.startActivityForResult(intent, RC_INVITE);
                                            } else
                                                Diagnostics.e(this, "ShortDynamicLink is null");
                                        } else
                                            Diagnostics.e(this, "link was not created");
                                    }
                                });
                }
                break;
    }
}

Manifest

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait" />

<activity android:name=".InfoActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".InvitationActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>

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

        <data android:host="hu.page.link" android:scheme="http"/>
        <data android:host="hu.page.link" android:scheme="https"/>
    </intent-filter>
</activity>

您可能想要检查启动意图的意图-目前看来,您正在启动InfoActivity ,安装该应用程序时的动态链接将进入InvitationActivity ,但是您的链接检查代码位于MainActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM