- Home /
Unity Like Facebook Page through app
Hi Unitarians,
I have an android app, and I have a button that I want my users to click to go to my facebook page and like it. If I use Application.OpenURL("https://www.facebook.com/youthquality");
, then it goes through the browser, which isn't ideal. I want it so that so it goes to the facebook app, if it is on the phone. I use the following code that works mostly:
void Facebook () {
#if UNITY_EDITOR
Application.OpenURL("https://www.facebook.com/xxx");
#elif UNITY_IPHONE
Debug.Log("Unity iPhone");
#else
if(checkPackageAppIsPresent("com.facebook.katana")) {
Application.OpenURL("fb://profile/xxx"); //there is Facebook app installed so let's use it
}
else {
Application.OpenURL("https://www.facebook.com/xxx"); // no Facebook app - use built-in web browser
}
#endif
}
private bool checkPackageAppIsPresent(string package)
{
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = ca.Call<AndroidJavaObject>("getPackageManager");
//take the list of all packages on the device
AndroidJavaObject appList = packageManager.Call<AndroidJavaObject>("getInstalledPackages",0);
int num = appList.Call<int>("size");
for(int i = 0; i < num; i++)
{
AndroidJavaObject appInfo = appList.Call<AndroidJavaObject>("get", i);
string packageNew = appInfo.Get<string>("packageName");
if(packageNew.CompareTo(package) == 0)
{
return true;
}
}
return false;
}
The problem with the code is that it opens the facebook app, but it goes tot he newsfeed, and not to my page. How would I make it go to my page?
My actual page has a set name that I put, not numbers. It is not a profile, it is a page.
Answer by federicogr · Dec 24, 2014 at 07:22 AM
I use only this line:
Application.OpenURL("fb://page/375158539307294");
And worked like a charm for me. One thing to note: while my page's address is https://www.facebook.com/foxy.the.game fb://page/foxy.the.game didn't work when I tested. Use your Facebook Page ID instead.
Go to https://www.facebook.com/yourpage/info?tab=page_info and at the bottom, you will see your Facebook Page ID
and if you are not the ad$$anonymous$$ of the page, of it in a browser, open page soure and search for pageID
is there any way to know if the the user already like the page?
Yes! check my code (the user has to be logged):
private void CheckUserLikes ()
{
FB.API(
query: "/me/likes/375158539307294",
method: Facebook.Http$$anonymous$$ethod.GET,
callback: APICallback);
}
private void APICallback (FBResult result)
{
if (DeserializeJSONPage(result.Text))
{
isFacebookFan = true;
}
}
It woks perfect! But what will this do if Facebook app is not in the device, or how to make open FB in browser if there is not FB app?
This was my solution (only android):
in my share button:
#if UNITY_ANDROID
if(checkPackageAppIsPresent("com.facebook.katana"))
{
Application.OpenURL("fb://page/375158539307294"); //there is Facebook app installed so let's use it
}
else
{
Application.OpenURL("https://www.facebook.com/foxy.the.game"); // no Facebook app - use built-in web browser
}
#endif
and the function:
#if UNITY_ANDROID
private bool checkPackageAppIsPresent(string package)
{
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject ca = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject package$$anonymous$$anager = ca.Call<AndroidJavaObject>("getPackage$$anonymous$$anager");
//take the list of all packages on the device
AndroidJavaObject appList = package$$anonymous$$anager.Call<AndroidJavaObject>("getInstalledPackages",0);
int num = appList.Call<int>("size");
for(int i = 0; i < num; i++)
{
AndroidJavaObject appInfo = appList.Call<AndroidJavaObject>("get", i);
string packageNew = appInfo.Get<string>("packageName");
if(packageNew.CompareTo(package) == 0)
{
return true;
}
}
return false;
}
#endif
Ins$$anonymous$$d of UNITY_ANDROID now below works
UNITY_ANDROID && !UNITY_EDITOR
Here is the function:
public static bool DeserializeJSONPage (string response)
{
var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object nameH;
return responseObject.TryGetValue("data", out nameH);
}
facebook sdk 7.8 does not support FBResult and resutl.Text so, what can i use?
my code.. if(isLoggedIn) { //facebook like FB.API ("/me/likes?fields=1065154363548853",Http$$anonymous$$ethod.GET,APICallback); } private void APICallback (IResult result) {
if(DeserializeJSONPage(result.RawResult))
{
isFacebookFan = true;
Debug.Log (result.ToString());
if(isFacebookFan)
{
isLiked.text = "You are liked";
}
else
{
isLiked.text = "You are not liked";
}
}
}
Answer by tim-bluenose · Feb 07, 2018 at 04:59 PM
We found that /page/ opened the facebook app, but not the proper page.
We had to use /profile/ instead. Perhaps it has something to do with the type of page being linked to?
Application.OpenURL("fb://profile/XXXXXXX");