[英]How to send app requests to friends through Facebook Android SDK
Currently I'm developing an Android app for that I'm using the Facebook SDK. 目前我正在开发一款Android应用程序,因为我正在使用Facebook SDK。 It's working fine for posting messages to the wall etc., but through this SDK I'm unable to send an app request to others. 它可以很好地将消息发布到墙上等,但是通过这个SDK,我无法向其他人发送应用请求。
Can anyone help me out? 谁能帮我吗?
here is my code snippet: 这是我的代码片段:
Bundle params = new Bundle();
params.putString("message", getString(R.string.request_message));
Utility.mFacebook.dialog(Hackbook.this, "apprequests", params, new AppRequestsListener());
and AppRequestsListener : 和AppRequestsListener :
public class AppRequestsListener extends BaseDialogListener {
@Override
public void onComplete(Bundle values) {
Toast toast = Toast.makeText(getApplicationContext(), "App request sent", Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onFacebookError(FacebookError error) {
Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast toast = Toast.makeText(getApplicationContext(), "App request cancelled", Toast.LENGTH_SHORT);
toast.show();
}
}
As of SDK version 3.0 you use a WebDialog. 从SDK 3.0版开始,您使用WebDialog。 Here is an example of how to create one using the provided Builder that uses all of the available parameters: 以下是如何使用提供的使用所有可用参数的构建器创建一个示例:
private void sendRequestDialog() {
Bundle params = new Bundle();
params.putString("title", "Send a Request");
params.putString("message", "Learn how to make your Android apps social");
params.putString("to", "12343543,32423534"); // comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.
params.putString("data",
"{\"badge_of_awesomeness\":\"1\"," +
"\"social_karma\":\"5\"}"); // any additional data
WebDialog requestsDialog = (
new WebDialog.RequestsDialogBuilder(getActivity(), Session.getActiveSession(), params))
.setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values, FacebookException error) {
// do something, e.g. show toast message
}
})
.build();
requestsDialog.show();
}
Reference: Facebook SDK 3.0 for Android: Send Requests 参考: Facebook SDK 3.0 for Android:发送请求
Using Facebook Api 3.0 使用Facebook Api 3.0
1. Send friend request 1.发送好友请求
Bundle params = new Bundle();
params.putString("message", "Learn how to make your Android apps social");
RequestsDialogBuilder builder = new RequestsDialogBuilder(MainActivity.this,
Session.getActiveSession(), params);
builder.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();
}
}
}
});
WebDialog requestDialog = builder.build();
requestDialog.show();
2. Send app request 2.发送应用请求
Bundle parameters = new Bundle();
parameters.putString("message", "Send Request");
WebDialog.Builder builder = new Builder(MainActivity.this, Session.getActiveSession(),
"apprequests", parameters);
builder.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();
}
}
}
});
WebDialog webDialog = builder.build();
webDialog.show();
The android sdk has Dialogs which you can use, and when you open a dialog you specify which dialog you want to open. android sdk有您可以使用的Dialogs ,当您打开一个对话框时,您可以指定要打开的对话框。
You can see the list of available dialogs in the Dialogs documentation. 您可以在Dialogs文档中看到可用对话框的列表。 One of the dialogs is the Requests Dialog and you can open that from the android sdk as well, something like: 其中一个对话框是Requests Dialog ,您也可以从android sdk打开它,例如:
Facebook facebook = new Facebook("YOUR_APP_ID");
....
Bundle params = new Bundle();
params.putString("title", "invite friends");
facebook.dialog(this, "apprequests", params, new DialogListener() {
@Override
public void onComplete(Bundle values) {}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
You can add more parameters for this dialog, use the documentation to see what it is you need. 您可以为此对话框添加更多参数,使用文档查看您需要的内容。
Ok, check out this code: 好的,看看这段代码:
Bundle paramsOut = new Bundle(), paramsIn = this.getIntent().getExtras();
paramsOut.putString("message", paramsIn.getString("message"));
this.facebook.dialog(this, "apprequests", paramsOut, new InviteListener(this));
I use it and it works well for me, the app request is being sent and the user receives it. 我使用它,它适用于我,应用程序请求正在发送,用户收到它。 Since your code is pretty similar, it is safe to assume that the problem is with what's different, and so you should post the code to what is different. 由于您的代码非常相似,因此可以安全地假设问题与不同之处有关,因此您应该将代码发布到不同的代码中。
So, what's in that AppRequestsListener of yours? 那么,你的AppRequestsListener中有什么? Saying that it just shows a popup does not help me to help you. 说它只是显示一个弹出窗口并没有帮助我帮助你。 Also, what is this *Hackbook"? is it an activity? 另外,这个* Hackbook是什么?它是一项活动吗?
this code working fine for me in Facebook Sdk 3.0 这段代码在Facebook Sdk 3.0中对我很好
private void sendRequestDialog(final String userId) {
Bundle params = new Bundle();
params.putString("title", "Invite Friend");
params.putString("message", "has invite has you to try out " +
"The perfect gamified experience for today's smart and social Cricket fan " +
"Download now on your ANDROID device!");
// comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.
params.putString("to", userId); // your friend id
WebDialog requestsDialog = ( new WebDialog.RequestsDialogBuilder(MainActivity.this,
Session.getActiveSession(), params)).setOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(Bundle values, FacebookException error) {
// Auto-generated method stub
if (error != null) {
if (error instanceof FacebookOperationCanceledException) {
Toast.makeText(MainActivity.this.getApplicationContext(),
"Request cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this.getApplicationContext(),
"Network Error", Toast.LENGTH_SHORT).show();
}
} else {
final String requestId = values.getString("request");
if (requestId != null) {
Toast.makeText(MainActivity.this.getApplicationContext(),
"Request sent", Toast.LENGTH_SHORT).show();
Log.i("TAG", " onComplete req dia ");
} else {
Toast.makeText(MainActivity.this.getApplicationContext(),
"Request cancelled", Toast.LENGTH_SHORT).show();
}
}
}
}).build();
requestsDialog.show();
}
I think you are missing the code in onComplete where you actually send the request. 我认为你错过了实际发送请求的onComplete中的代码。 All you have in onComplete is a toast that is why you receive a message saying the request was sent. 你在onComplete中所拥有的只是一个祝酒词,这就是你收到一条消息说你发送了请求的原因。 You need a return Id to actually send the request. 您需要返回ID才能实际发送请求。
public void onComplete(Bundle values) {
final String returnId = values.getString("request");
if (returnId != null) {
Toast.makeText(getApplicationContext(),
"Request sent " + returnId,
Toast.LENGTH_SHORT).show();
}
}
You have to actually send out the values onComplete. 您必须实际发送值onComplete。
Using Facebook Api < 3.0 - Send app request 使用Facebook Api <3.0 - 发送应用请求
reference: https://stackoverflow.com/users/1237937/kirit 参考: https : //stackoverflow.com/users/1237937/kirit
public void run() {
Bundle parameters = new Bundle();
parameters.putString("message", "Send Request");
WebDialog.Builder builder = new WebDialog.Builder(ConnectionSearchFacebook.this, facebookConnector.getFacebook().getSession(),
"apprequests", parameters);
builder.setOnCompleteListener(new WebDialog.OnCompleteListener() {
@Override
public void onComplete(Bundle values, FacebookException error) {
if (error != null){
if (error instanceof FacebookOperationCanceledException){
Toast.makeText(ConnectionSearchFacebook.this,"Request cancelled",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(ConnectionSearchFacebook.this,"Network Error",Toast.LENGTH_SHORT).show();
}
}
else{
final String requestId = values.getString("request");
if (requestId != null) {
Toast.makeText(ConnectionSearchFacebook.this,"Request sent",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(ConnectionSearchFacebook.this,"Request cancelled",Toast.LENGTH_SHORT).show();
}
}
}
});
WebDialog webDialog = builder.build();
webDialog.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.