3

我试图在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。

应用信息中心配置 enter image description here

回答

1

这是一个问题,我花了整整一天的时间。感谢Boban

这是Facebook的bug。只需添加移动Web应用程序中的仪表盘,你会得到request_ids

Android app : Handling app request posted on Facebook

+0

你好尼克,我有同样的问题,你可以检查我的问题[http://stackoverflow.com/questions/24324963/unable-to-get-facebook-incoming-requests](http://stackoverflow.com/问题/ 24324963 /无法获取Facebook的传入请求)也。如果可能,那么请帮助我。 – Kunu 2014-06-24 06:07:14

1

我想你也没有忘记requestId属性:

private String requestId; 

难道你还要检查在onActivityCreated方法intentUrirequestIdParamarray[0]值是多少?

@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控制板设置配置的 类将被调用和 请求的信息将通入意图数据。这些数据将 有如下信息:

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

但是,你没有得到request_ids参数。在我看来,有一些错误配置如下: App Dashboard settings

什么了投放在这些领域?它是否符合您的实际应用程序树状结构?

编辑2:

类名称:这是你想要 Facebook上推出的主要活动的类名。

你的Login.java有什么?

它应该是com.pricus.expensemanagement.MainActivity

确保你正确地按照this first tutorial about login,特别是这一步:

首先,使活动的子类FragmentActivity代替 活动:

public class MainActivity extends FragmentActivity { 
+0

已经将此代码添加到onActivityCreated – 2013-03-02 11:37:21

+0

不,我要问你打印上述命名变量的值。用“HERE”语句编辑后。做到这一点,并回答三个'log.d'的结果。 – 2013-03-02 11:43:12

+0

in logcat我得到空 – 2013-03-02 11:52:07