Hi, I’ve recently purchased prime31 social networking plugin and tried adding a Facebook and a Twitter button into a game.
Facebook button should just post a pre-defined message on the user’s wall, and Twitter should post a message on user’s behalf in his Twitter feed. The player is rewarded by either of the actions.
Below is all of the relevant code I added.
Facebook basically works, BUT if the user is initially not logged in, then the loginSucceededEvent doesn’t come, so the user has to press the Facebook button once again to actually post (both on Android and iOS).
Also, on Android, if the user tries to post on Facebook right after loggin in (second press on Facebook button), he would always get
java.io.FileNotFoundException: https://graph.facebook.com/me/feed
After restarting the game it posts ok.
Twitter has similar problem: it posts, but basically no events ever fire.
On Android twitterInitializedEvent is never fired. According to prime31 - Mobile game and app experts specializing in Unity games and plugins, I placed “http://www.google.com” into “Callback URL:” on twitter developer page for the app, but this didn’t change anything.
logcat shows:
I/Prime31 ( 2017): initializing Twitter service
I/Prime31-OAS( 2017): request token endpoint: https://api.twitter.com/oauth/request_token
and nothing else.
On iOS showOauthLoginDialog successfully opens the Twitter login page and TwitterBinding.postStatusUpdate creates the tweet, but
neither of these fire:
TwitterManager.loginSucceededEvent
TwitterManager.postSucceededEvent
TwitterManager.postFailedEvent
I guess there may just be something wrong with the way I am attaching delegates?
private void InitializeFacebook()
{
Debug.Log("InitializeFacebook");
FacebookAccess.init(FACEBOOK_APP_ID);
FacebookManager.loginSucceededEvent += OnFacebookLoggedIn;
FacebookManager.customRequestReceivedEvent += OnCustomFacebookEvent;
}
private void InitializeTwitter()
{
Debug.Log("InitializeTwitter");
#if UNITY_ANDROID
TwitterAndroidManager.twitterInitializedEvent += OnTwitterInitialized;
TwitterAndroid.init(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
TwitterAndroidManager.loginDidSucceedEvent += OnTwitterLoginAndroid;
#elif UNITY_IPHONE
TwitterBinding.init(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
TwitterManager.loginSucceededEvent += OnTwitterLoginIOS;
TwitterManager.postSucceededEvent += OnPostedOnTwitter;
TwitterManager.postFailedEvent += OnPostOnTwitterFailed;
#endif
}
private void CleanUpFacebook()
{
Debug.Log("CleanUpFacebook");
FacebookManager.loginSucceededEvent -= OnFacebookLoggedIn;
FacebookManager.customRequestReceivedEvent -= OnCustomFacebookEvent;
}
private void CleanUpTwitter()
{
Debug.Log("CleanUpTwitter");
#if UNITY_ANDROID
TwitterAndroidManager.twitterInitializedEvent -= OnTwitterInitialized;
TwitterAndroidManager.loginDidSucceedEvent -= OnTwitterLoginAndroid;
#elif UNITY_IPHONE
TwitterManager.loginSucceededEvent -= OnTwitterLoginIOS;
TwitterManager.postSucceededEvent -= OnPostedOnTwitter;
TwitterManager.postFailedEvent -= OnPostOnTwitterFailed;
#endif
}
public void OnFacebookLoggedIn()
{
Debug.Log("OnFacebookLoggedIn");
PostOnFacebook();
}
private void PostOnFacebook()
{
Debug.Log("PostOnFacebook");
#if UNITY_ANDROID
Facebook.instance.postMessageWithLinkAndLinkToImage(
"I'm playing this game. Check it out",
"https://play.google.com/store/apps/details?id=com.nuclearfox.rotation",
"Steam Puzzle",
"http://www.frostball.com/games_screens/Rotation_100_100.png",
"GooglePlay",
OnPostOnFacebookCompletionHandler);
#elif UNITY_IPHONE
Facebook.instance.postMessageWithLinkAndLinkToImage(
"I'm playing this game. Check it out",
"http://itunes.apple.com/us/app/rotation/id528259248",
"Steam Puzzle",
"http://www.frostball.com/games_screens/Rotation_100_100.png",
"AppStore",
OnPostOnFacebookCompletionHandler);
#else
this.sPopupMessage = "Facebook integration is only implemented for Android and iOS";
#endif
}
public void OnPostOnFacebookCompletionHandler(string sError, object data)
{
if (null != data)
ResultLogger.logObject(data);
if (null != sError)
{
Debug.LogError(sError);
this.sPopupMessage = "Error while connecting to Facebook. \nPlease retry later. \n\nError: " + sError + "... ";
return;
}
PlayerPrefs.SetInt("has_posted_on_facebook", 1);
this.sPopupMessage = "Thanks for telling your friends about the game!";
}
public void OnCustomFacebookEvent(object obj)
{
if (null != obj)
ResultLogger.logObject(obj);
}
private bool bIsTwitterInitialized = false;
public void OnTwitterInitialized()
{
Debug.Log("OnTwitterInitialized");
bIsTwitterInitialized = true;
}
public void OnTwitterLoginAndroid(string sUserScreenName)
{
Debug.Log("OnTwitterLoginAndroid: " + sUserScreenName);
PostOnTwitter();
}
public void OnTwitterLoginIOS()
{
Debug.Log("OnTwitterLoginIOS");
PostOnTwitter();
}
private void PostOnTwitter()
{
Debug.Log("PostOnTwitter");
#if UNITY_ANDROID
//Application.CaptureScreenshot("screenshot.png");
//string pathToImage = Application.persistentDataPath + "/" + "screenshot.png";
//byte[] bytes = System.IO.File.ReadAllBytes(pathToImage);
//TwitterAndroid.postUpdateWithImage("test update from Unity!", bytes);
TwitterAndroid.postUpdate("I am currently playing Steam Puzzle. Check it out: https://play.google.com/store/apps/details?id=com.nuclearfox.rotation");
// since on Android no event is fired when message is posted on Twitter,
// immediately assume that the user has posted successfully
PlayerPrefs.SetInt("has_posted_on_twitter", 1);
#elif UNITY_IPHONE
TwitterBinding.postStatusUpdate("I am currently playing Steam Puzzle. Check it out: http://itunes.apple.com/us/app/rotation/id528259248");
// on iOS an event will fire when message is posted on Twitter
#endif
}
public void OnPostedOnTwitter()
{
Debug.Log("OnPostedOnTwitter");
PlayerPrefs.SetInt("has_posted_on_twitter", 1);
}
public void OnPostOnTwitterFailed(string sError)
{
Debug.Log("OnPostOnTwitterFailed");
this.sPopupMessage = "Failed to post on Twitter! \nPlease retry later\n\nError: " + sError;
}
void OnGUI()
{
GUI.skin = this.guiSkin;
// if a popup is shown, draw it in front of everything
if (null != this.sPopupMessage)
{
GUI.DrawTexture(this.rectDialog, this.texBgDialog);
Game.DrawLabel(this.rectDlgText, this.sPopupMessage, this.guiSkin);
if (GUI.Button(this.rectDlgOK, "", this.guiSkin.customStyles[CustomStyle.BTN_OK]))
{
if (Game.isSoundOn)
audio.PlayOneShot(this.audioButtonPress);
this.sPopupMessage = null;
}
return;
}
if (GUI.Button(this.rectFacebookBtn, "", this.guiSkin.customStyles[CustomStyle.BTN_FACEBOOK]))
{
if (Game.isSoundOn)
audio.PlayOneShot(this.audioButtonPress);
#if UNITY_ANDROID || UNITY_IPHONE
// post immediately if already logged in, otherwise log in
if (!FacebookAccess.isSessionValid())
{
Debug.Log("User not logged into Facebook, attempting login");
FacebookAccess.loginWithRequestedPermissions( new string [] {"publish_stream", "publish_actions"} );
}
else
{
PostOnFacebook();
}
#else
this.sPopupMessage = "Facebook integration is only implemented for Android and iOS";
#endif
}
if (GUI.Button(this.rectTwitterBtn, "", this.guiSkin.customStyles[CustomStyle.BTN_TWITTER]))
{
if (Game.isSoundOn)
audio.PlayOneShot(this.audioButtonPress);
#if UNITY_ANDROID
if (!this.bIsTwitterInitialized)
{
this.sPopupMessage = "Twitter not initialized!";
return;
}
if (!TwitterAndroid.isLoggedIn())
{
Debug.Log("User not logged into Twitter, attempting login");
TwitterAndroid.showLoginDialog();
}
else
{
PostOnTwitter();
}
#elif UNITY_IPHONE
if (!TwitterBinding.isLoggedIn())
{
Debug.Log("User not logged into Twitter, attempting login");
TwitterBinding.showOauthLoginDialog();
}
else
{
PostOnTwitter();
}
#else
this.sPopupMessage = "Twitter integration is only implemented for Android and iOS";
#endif
}
..
}
I am calling
InitializeFacebook() and InitializeTwitter() in Start()
and CleanUpFacebook() and CleanUpTwitter() just before calling Application.Quit()
Thanks!