3

我正在尝试处理 facebook 中的传入通知。

参考: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);

}
}

日志猫

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。

应用仪表板配置 在此处输入图像描述

4

2 回答 2

1

这是我花了一整天时间解决的问题。感谢博班

这是 Facebook 错误。只需在 App Dashboard 中添加 Mobile Web,您将获得 request_ids

Android 应用:处理在 Facebook 上发布的应用请求

于 2013-04-15T08:11:55.390 回答
1

我想你没有忘记这个requestId属性:

private String requestId;

您还可以检查intentUri,requestIdParam和方法array[0]中的值onActivityCreated吗?

@Override
public void onActivityCreated(Bundle savedInstanceState) { // <--- in THIS method
    super.onActivityCreated(savedInstanceState);
    Log.d(TAG, "MainFragment.onActivityCreated()");

    // Check for an incoming notification. Save the info
    Uri intentUri = getActivity().getIntent().getData();
    Log.d(TAG, "intentUri: " + intentUri.toString()); // <--- HERE
    if (intentUri != null) {
        String requestIdParam = intentUri.getQueryParameter("request_ids");
        Log.d(TAG, "requestIdParam: " + requestIdParam); // <--- HERE
        if (requestIdParam != null) {
            String array[] = requestIdParam.split(",");
            Log.d(TAG, "requestId: " + array[0]); // <--- HERE
            requestId = array[0];
        }
    }
}

请更新您的帖子,向我们展示整个班级。

编辑:

当用户点击 Android 版 Facebook 中的通知时,您在 App Dashboard 设置中配置的类将被调用,请求信息将传递到 Intent 数据中。数据将包含以下信息:

target_url=[URL]/?request_ids=[COMMA_SEPARATED_REQUESTIDs]
    &ref=notif&fb_source=notification
    &app_request_type=user_to_user

但是,您没有得到request_ids参数。在我看来,以下配置有问题: 应用仪表板设置

你在这些领域放了什么?它与您的实际应用树状结构相符吗?

编辑2:

类名:这是您希望 Facebook 启动的主要活动的类名。

你的里面有什么Login.java

应该是com.pricus.expensemanagement.MainActivity

确保您已正确遵循有关 login 的第一个教程,尤其是这一步:

首先,使活动子类 FragmentActivity 而不是 Activity:

public class MainActivity extends FragmentActivity {
于 2013-03-02T11:28:31.387 回答