Current Month Facebook Friends Birthdays in Android

How to get FB friends birthday in Android

Instead of these two lines of code:

 sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0, 1);
sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(0, 2);

Use below two lines:

sections[i] = jsonArray.getJSONObject(i).getString("name").substring(0);
sections[i] = jsonArray.getJSONObject(i).getString("birthday").substring(1);

FQL Query to get Facebook Friends Birthdays in next 30 days using Android

Well, first of all, make sure to ask for friends_birthday permission, without it you can't get your friends birthday dates.

If you are not familiar with the Graph API Explorer, you should - you can test FQL queries to find problems.

Click the "Get Access Token" and in "Friends Data Permissions" check the "friends_birthday"
and you are good to go testing the query.

Second, all the birthdays are stored with the actual birth year, so asking for above today in FQL query will always return nothing.
to "fix" the date - you can actually select a portion of the birthday and this year to it like so:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'

And here comes the problem:

The selected field is not part of the user table, and you can't sort by it, or even use WHERE on it.
My guess is that you have to get all your friends birthday and use JAVA to filter the data - It should be easy since the date in the custom selected field has 2013 instead of the real year.

Feel free to Test the FQL with this link.

EDIT #1

I want to be clear on something, the original query you wish to convert will not

fetch list of Facebook friends those birthdays in next 30 days

When replacing the values manually in the query I saw that it returns all friends with birthday between 2 given days in the same month.
It does not find the all the friends who has a birthday in the next 30 days.

EDIT #2

I have found a way to get all friends with birthday in this month and the next - which is much better the to go over all the friends list.

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')

This should become much easier to go over the list and filter the ones before today and no more then 30 days from today - this is the best solution I can think of.

EDIT #3
I can actually filter using the same query from edit #2 to get all the ones after today, ordered by it:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
AND birthday_date > '02/23/2013'
ORDER BY birthday_date

And if you want to limit by 10:

SELECT name, birthday, birthday_date, concat(substr(birthday_date,0,6),"2013") FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND (substr(birthday_date,0,2)='02' OR substr(birthday_date,0,2)='03')
AND birthday_date > '02/23/2013'
ORDER BY birthday_date
LIMIT 10

Edit #4

Much more simple if you calculate the dates in Java and pass it to the query

SELECT name, birthday, birthday_date FROM user 
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND birthday_date > '02/23/2013'
AND birthday_date < '04/24/2013'
ORDER BY birthday_date ASC

EDIT #5

Some Java code:

Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String today_formatted = formatter.format(today);
cal.add(Calendar.DATE, 30);
String today_plus30_formatted = formatter.format(cal.getTime());

String query =
"SELECT name, birthday, birthday_date FROM user
WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())
AND birthday_date != 'null'
AND birthday_date > '" + today_formatted + "'
AND birthday_date < '" + today_plus30_formatted + "'
ORDER BY birthday_date ASC";

I have not tested it, don't have Java installed :-)

sync facebook friends birthdays with my android app

first create object and veriable for facebook:

private static String FACEBOOK_APP_ID = "492429660800628";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;

after OnCreate Method :

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);

facebook = new Facebook(FACEBOOK_APP_ID);
mAsyncRunner = new AsyncFacebookRunner(facebook);

loginFacebook();//this method when called when you required..

}

private void loginFacebook() {

if (!facebook.isSessionValid()) {

facebook.authorize(this, new String[] { "email", "publish_stream",
"read_stream" }, new LoginDialogListener());

} else {

getProfileInformation();

}

}

class LoginDialogListener implements DialogListener {

public void onComplete(Bundle values) {
try {

getProfileInformation();

} catch (Exception error) {
Toast.makeText(LoginActivity.this, error.toString(),
Toast.LENGTH_SHORT).show();
}
}

public void onFacebookError(FacebookError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}

public void onError(DialogError error) {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}

public void onCancel() {
Toast.makeText(LoginActivity.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
}

please try this method after login facebook:

public void getProfileInformation() {

try {

JSONObject profile = Util.parseJson(facebook.request("me"));
Log.e("Profile", "" + profile);

mUserId = profile.getString("id");
mUserToken = facebook.getAccessToken();
mUserName = profile.getString("name");
mUserEmail = profile.getString("email");

runOnUiThread(new Runnable() {

public void run() {

Log.e("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);

Toast.makeText(getApplicationContext(),
"Name: " + mUserName + "\nEmail: " + mUserEmail,
Toast.LENGTH_LONG).show();

}

});

} catch (FacebookError e) {

e.printStackTrace();
} catch (MalformedURLException e) {

e.printStackTrace();
} catch (JSONException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

using latest facebook android SDK
from developer.facebook linkhttps://developers.facebook.com/docs/android/fetch-user-data/



Related Topics



Leave a reply



Submit