gpt4 book ai didi

android - 无法获取 Facebook 传入请求

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:54 25 4
gpt4 key购买 nike

我正在尝试在我的 Facebook 安卓游戏应用程序中实现发送数据和接受该数据。我正在关注 https://developers.facebook.com/docs/android/send-requests/#notifications教程。我可以发送请求,但是我无法在接收方接受数据。

我的 intentUri 每次都返回 null,所以我无法获取 requestIds。

我在接收方获取数据的代码:

intentUri = MainActivity.this.getIntent().getData();
if (intentUri != null) {
String requestIdParam = intentUri.getQueryParameter("request_ids");
if (requestIdParam != null) {
String array[] = requestIdParam.split(",");
requestId = array[0];
Log.i("Request id: ", "Request id: " + requestId);

}else{
Log.i("Request id: ", "null" );

}
} else {
Log.i("intentUri", "null"); //**Always printing this part**
}

调用requestData方法的onSessionStateChange()方法

private void onSessionStateChange(Session session, SessionState state,Exception exception) {
if (state.isOpened() && requestId != null) {
getRequestData(requestId);
requestId = null;
}
}

我正在使用以下方法发送请求:

private void sendRequestDialog() {
Bundle params = new Bundle();
params.putString("message",
"Learn how to make your Android apps social");
params.putString("data", "{\"badge_of_awesomeness\":\"1\","
+ "\"social_karma\":\"5\"}");
WebDialog requestsDialog = (new WebDialog.RequestsDialogBuilder(
MainActivity.this, Session.getActiveSession(), params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values,
FacebookException error) {
if (error != null) {
if (error instanceof FacebookOperationCanceledException) {
Toast.makeText(MainActivity.this,
"Request cancelled", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MainActivity.this,
"Network Error", Toast.LENGTH_SHORT)
.show();
}
} else {
final String requestId = values
.getString("request");
if (requestId != null) {
Toast.makeText(MainActivity.this,
"Request sent", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MainActivity.this,
"Request cancelled", Toast.LENGTH_SHORT)
.show();
}
}
}
}).build();
requestsDialog.show();
}

接受请求的代码如下:

private void getRequestData(final String inRequestId) {
// Create a new request for an HTTP GET with the
// request ID as the Graph path.
Request request = new Request(Session.getActiveSession(), inRequestId,
null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// Process the returned response
GraphObject graphObject = response.getGraphObject();
FacebookRequestError error = response.getError();
// Default message
String message = "Incoming request";
if (graphObject != null) {
// Check if there is extra data
if (graphObject.getProperty("data") != null) {
try {
// Get the data, parse info to get the
// key/value info
JSONObject dataObject = new JSONObject(
(String) graphObject
.getProperty("data"));
// Get the value for the key -
// badge_of_awesomeness
String badge = dataObject
.getString("badge_of_awesomeness");
// Get the value for the key - social_karma
String karma = dataObject
.getString("social_karma");
// Get the sender's name
JSONObject fromObject = (JSONObject) graphObject
.getProperty("from");
String sender = fromObject
.getString("name");
String title = sender + " sent you a gift";
// Create the text for the alert based on
// the sender
// and the data
message = title + "\n\n" + "Badge: "
+ badge + " Karma: " + karma;
} catch (JSONException e) {
message = "Error getting request info";
}
} else if (error != null) {
message = "Error getting request info";
}
}
Toast.makeText(MainActivity.this, message,
Toast.LENGTH_LONG).show();
deleteRequest(inRequestId);
}
});
// Execute the request asynchronously.
Request.executeBatchAsync(request);
}

注意:我只是使用简单的 Activity ,我没有使用 fragment

最佳答案

getIntent() 方法返回启动 Activity 的 Intent 。您可以尝试覆盖 Activity 方法

protected void onNewIntent(Intent intent)

然后打电话

setIntent(Intent Intent )

从那里开始。

您还可以尝试使用以下方式请求您的数据:

Intent intent = MainActivity.this.getIntent();
Bundle bundle = intent.getExtras();
intentUri = (Uri)bundle.get(Intent.EXTRA_TEXT);

除此之外,您在哪里以及如何调用 intent.putExtra("request_ids", data)

不确定我是否完全理解你的问题,

无论如何,祝你好运。

编辑:

您可能希望使用 Intent.EXTRA_STREAM 而不是 Intent.EXTRA_TEXT,具体取决于您存储数据的方式

关于android - 无法获取 Facebook 传入请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24324963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com