Fasebook邀请可以指定fb id进行一个或者多个邀请,也可以默认随便几个好友进行邀请。
在之前的实现邀请功能时,一般先进行获取可邀请的好友列表(除了已经在使用APP好友),再进行指定邀请,如果游戏邀请成功后进行奖励。
但从2018年4月4日开发,fb禁止获取可邀请好友列表(invitable_friends)
User Invitable Friends
This edge was deprecated on April 4th, 2018, and can no longer be accessed.
所以邀请流程需求变动,先了解如何进行邀请:
引用库:implementation 'com.facebook.android:facebook-android-sdk:4.32.0'
官方拉起邀请界面代码,来源点击这里:
GameRequestDialog requestDialog;
CallbackManager callbackManager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
requestDialog = new GameRequestDialog(this);
requestDialog.registerCallback(callbackManager,
new FacebookCallback<GameRequestDialog.Result>() {
public void onSuccess(GameRequestDialog.Result result) {
String id = result.getId();
}
public void onCancel() {}
public void onError(FacebookException error) {}
}
);
}
private void onClickRequestButton() {
GameRequestContent content = new GameRequestContent.Builder()
.setMessage("Come play this level with me")
.build();
requestDialog.show(content);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
其中GameRequestContent类可以设置以下属性:
参数名称 | 说明 | 是否必需 |
---|---|---|
app_id |
应用的唯一标识符。 |
是 |
redirect_uri |
在发送方点击对话框按钮后重新定向到的网址。可让发送方在发送请求后返回游戏。出于安全考虑,指定的 |
使用网址重新定向时必须提供 |
to |
此参数为用户 |
否 |
message |
要在请求中发送的纯文本消息。此文本会在请求的应用中心视图中显示,但不会在通知图标中显示 |
是 |
action_type |
在定义有关请求性质的附加背景时使用。可能的值包括 |
如果设置了 |
object_id |
待发送对象的开放图谱对象编号。 |
如果 |
filters |
此参数控制显示好友选择工具时可以看到的好友组。如果留空,好友选择工具会显示用户的所有 Facebook 好友。指定 |
否 |
suggestions |
将作为首选推荐好友而包含在对话框中的一组用户编号。注意:此参数仅面向移动设备提供,不可与 |
否 |
exclude_ids |
将从对话框中排除的一组用户编号。如果从对话框中排除某些用户,他们将不会在好友选择工具中显示。注意:移动版 SDK 不支持此参数并会忽略此参数。 |
否 |
max_recipients |
整数值,用于指定发送方可在好友选择工具中选择的最大好友数量。此参数不受移动设备的支持。 |
否 |
data |
可以传递用于追踪的附加自由格式数据。此参数会作为所创建的请求对象的一部分存储。最大长度为 255 个字符。 |
否 |
title |
对话框的标题。最大长度为 50 个字符。 |
否 |
从上可知指定 app_users 后,好友选择工具仅会显示身份为应用现有用户的好友。或者,指定 app_non_users,发送方仅会看到之前未验证应用的好友。
邀请回调FacebookCallback中,官方代码有问题。并没有result.getId();
FacebookCallback c = new FacebookCallback<GameRequestDialog.Result>() {
public void onSuccess(GameRequestDialog.Result result) {
String id = result.getRequestId();
Toasts.makeText(ShareByFacebookActivity.this, "RequestId:" + id);
List<String> list = result.getRequestRecipients();
for (int i = 0; i < list.size(); i++) {
Print.out("Invite fb id:" + list.get(i)); // 邀请了那几个fb好友(fb id)
}
}
public void onCancel() {
}
public void onError(FacebookException error) {
}
};
上面代码可以看出,现在邀请是知道邀请了那几个好友,并返回对应的fb id,游戏可以记录邀请过的id不进行奖励即可。
获取现有用户的好友
要好友头像,增加参数parameters.putString("fields", "id,name,picture")即可:
GraphRequest request = GraphRequest.newMyFriendsRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONArrayCallback() {
@Override
public void onCompleted(JSONArray list, GraphResponse arg1) {
// 应用好友列表
List<FbInvitableUser> userList = new ArrayList<FbInvitableUser>();
if (null != list) {
// Print.out("reqFacebookFriends = " + list.toString());
try {
for (int i = 0; i < list.length(); i++) {
JSONObject o = list.getJSONObject(i);
FbInvitableUser u = new FbInvitableUser();
u.setId(o.getString("id"));
u.setName(o.getString("name"));
JSONObject picture = o.getJSONObject("picture");
JSONObject data = picture.getJSONObject("data");
u.setPicture(data.getString("url"));
userList.add(u);
}
} catch (Exception e) {
// Print.i("getGameFriends - Exception", Log.getStackTraceString(e));
}
}
if (_getGameFriendsListener != null) {
_getGameFriendsListener.onCompleted(_requestCode, userList);
}
finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("limit", "5000");
parameters.putString("fields", "id,name,picture");
request.setParameters(parameters);
request.executeAsync();
结果list:
[
{
"id": "495116670667368",
"name": "Fei--",
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "https://lookaside.facebook.com/platform/profilepic/?asid=495116670667368&height=50&width=50&ext=1523851601&hash=AeR5ZoLja49AQpGF",
"width": 50
}
}
},
{
"id": "136891093344957",
"name": "周--",
"picture": {
"data": {
"height": 50,
"is_silhouette": false,
"url": "https://lookaside.facebook.com/platform/profilepic/?asid=136891093344957&height=50&width=50&ext=1523851601&hash=AeRS5-swRfyUb83s",
"width": 50
}
}
}
]
(注:您想在调用结束前暂时阻止,可以调用 request.executeAndWait()
。)
参数可以使用Facebook API 探索工具 看看参数可行性: