我正在开发一个可以共享并获得奖励的应用程序,因此我使用了动态链接的概念,以便将邀请者的信息存储在该链接中,并在以后得到奖励。但PendingDynamicLinkData在引用后安装该应用程序时返回null。
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);
}
});
}
MainActivity中“邀请”按钮上的onClick事件
void onInviteClicked() {
JSONObject json = jsonObjectCreator.createJSONObjectForSendingUserId(getUserId());
String jsonString = json.toString();
getterReferenceId = new GetFromServer(this, GET_REFERENCE_URL, jsonString);
getterReferenceId.execute();
}
GetFromServer类
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;
}
}
清单
<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>
答案 0 :(得分:0)
您可能要检查启动意图,目前看来您正在启动InfoActivity
,而安装应用程序后的动态链接将进入InvitationActivity
,但是您的链接检查代码位于MainActivity
中。