我试图在Facebook中处理传入的通知。请求ID为null处理Facebook中的传入通知android sdk
裁判:http://developers.facebook.com/docs/howtos/androidsdk/3.0/app-link-requests/
代码:
public class MainFragment extends Fragment{
private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;
private String requestId;
private TextView username;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "MainFragment.onCreate()");
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.d(TAG, "MainFragment.onCreateView()");
View view = inflater.inflate(R.layout.login,container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.loginButton);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
authButton.setFragment(this);
username = (TextView) view.findViewById(R.id.textusename);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "MainFragment.onActivityCreated()");
// Check for an incoming notification. Save the info
// Parse any incoming notifications and save
Uri intentUri = getActivity().getIntent().getData();
if (intentUri != null) {
String requestIdParam = intentUri.getQueryParameter("request_ids");
if (requestIdParam != null) {
String array[] = requestIdParam.split(",");
requestId = array[0];
}
}
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Check if the user is authenticated and
// an incoming notification needs handling
Log.d(TAG,"MainFragment.onSessionStateChange()");
Log.d(TAG, "Request id : "+requestId);
if (state.isOpened() && requestId != null) {
Log.d("request id", requestId);
getRequestData(requestId);
Toast.makeText(getActivity().getApplicationContext(), "Incoming request", Toast.LENGTH_SHORT).show();
requestId = null;
}
if (state.isOpened())
{
Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
Request.executeMeRequestAsync(Session.getActiveSession(), new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
username.setText(user.getName());
}
}
});
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void getRequestData(String requestid)
{
Request request = new Request(Session.getActiveSession(), requestid, null, HttpMethod.GET, new Request.Callback() {
@Override
public void onCompleted(Response response) {
// Process the returned response
GraphObject graphObject = response.getGraphObject();
FacebookRequestError error = response.getError();
Log.d(TAG, response.toString());
// 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(graphObject.getProperty("data").toString());
// 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 (Exception e) {
e.printStackTrace();
}
}
Toast.makeText(getActivity().getApplicationContext(),message,Toast.LENGTH_LONG).show();
}
}
});
Log.d(TAG, "Request : "+request.toString());
// Execute the request asynchronously.
Request.executeBatchAsync(request);
}
}
logcat的
03-02 17:40:20.443: D/MainFragment(947): MainFragment.onCreate()
03-02 17:40:20.463: D/MainFragment(947): MainFragment.onCreateView()
03-02 17:40:20.753: D/MainFragment(947): MainFragment.onActivityCreated()
03-02 17:40:28.623: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:28.623: D/MainFragment(947): Request id : null
03-02 17:40:30.673: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:30.673: D/MainFragment(947): Request id : null
03-02 17:40:30.823: D/MainFragment(947): MainFragment.onSessionStateChange()
03-02 17:40:30.833: D/MainFragment(947): Request id : null
现在我的问题是,当我点击通知后使用Facebook的Android应用我的应用程序开始,但我不能获取该通知的请求ID。
应用信息中心配置
你好尼克,我有同样的问题,你可以检查我的问题[http://stackoverflow.com/questions/24324963/unable-to-get-facebook-incoming-requests](http://stackoverflow.com/问题/ 24324963 /无法获取Facebook的传入请求)也。如果可能,那么请帮助我。 – Kunu 2014-06-24 06:07:14