×
Namespaces

Variants
Actions
(Difference between revisions)

Facebook Chat Client for Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
Vaishali Rawat (Talk | contribs)
(Vaishali Rawat - - Introduction)
chintandave_er (Talk | contribs)
m (Chintandave er - - Steps and Application flow)
Line 34: Line 34:
 
* Create a standard Windows Phone project using Visual Studio  
 
* Create a standard Windows Phone project using Visual Studio  
 
* Download the library and add to your project reference  
 
* Download the library and add to your project reference  
* Authenticate your client application with {{Icode|xmpp_login}} extended permission. You can get the complete application authentication process in the example source code or follow the below articles for better understanding.  
+
* Authenticate your client application with {{Icode|xmpp_login}} extended permission. You can get the complete application authentication process in the example source code or follow the below articles for better understanding.
* [[Facebook authentication in Windows Phone application]]
+
:*[[Facebook authentication in Windows Phone application]]
* [[Integrate Facebook to Your Windows Phone Application]]
+
:*[[Integrate Facebook to Your Windows Phone Application]]
 
* Get the {{Icode|access_token}} and save it for future use.  
 
* Get the {{Icode|access_token}} and save it for future use.  
 
* Follow the ''Code Behind'' section below to make use of the library.
 
* Follow the ''Code Behind'' section below to make use of the library.

Revision as of 12:07, 23 September 2013

This article demonstrate the use of Facebook Chat library to create a Facebook chat client application using X-FACEBOOK-PLATFORM SASL authentication mechanism.

WP Metro Icon Messages.png
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
Devices(s): Nokia Lumia 820,920
Compatibility
Dependencies: Facebook API
Platform Security
Capabilities: ID_CAP_NETWORKING
Article
Created: somnathbanik (19 Sep 2013)
Last edited: chintandave_er (23 Sep 2013)

Contents

Introduction

Facebook Chat client used Jabber/XMPP service to connect with Facebook server. You can authenticate Facebook Chat client using one of these two method : X-FACEBOOK-PLATFORM and DIGEST-MD5. In this example we will use X-FACEBOOK-PLATFORM mechanism to connect to Facebook Chat server. This process is recommended by Facebook and in order to connect using this mechanism, the user must first log in to the developed application and grant the xmpp_login extended permission. Follow the application authentication process to get an access_token with the xmpp_login extended permission. Once we have the access_token the application can log in to Facebook Chat via Jabber using the X-FACEBOOK-PLATFORM mechanism. For more details about Facebook Chat client authentication and implementation visit Facebook Chat API.

Implementation

Steps and Application flow

  • Create a standard Windows Phone project using Visual Studio
  • Download the library and add to your project reference
  • Authenticate your client application with xmpp_login extended permission. You can get the complete application authentication process in the example source code or follow the below articles for better understanding.
  • Get the access_token and save it for future use.
  • Follow the Code Behind section below to make use of the library.

Note.pngNote: The UI creation of the application is not discussed in this article as it’s not the main focus of this example. You can get a basic UI implementation in the example source code or create a beautiful UI of your choice.

Create a standard Windows Phone Project

Let’s create a standard Windows Phone Application Project

  • Launch Visual Studio
  • Click on File
  • Click on New Project
  • Select Windows Phone App (Visual C# Template)
  • Add Name and Location of the project
  • Click OK to create the project.

Code Behind

  • Once you are done adding the library in the project reference add the using statement to call the APIs.
using XFacebook.Chat.WindowsPhone.XFACEBOOK;
  • Create an instance of XFacebook class in App.xaml.cs file.
public static IXFacebook iXFacebook = new XFacebook.Chat.WindowsPhone.XFACEBOOK.XFacebook();
  • Call the event OnLoadedOnlineFriends to load all online Facebook friends and display on a list box.
App.iXFacebook.OnLoadedOnlineFriends += iXFacebook_OnLoadedOnlineFriends;
 
void iXFacebook_OnLoadedOnlineFriends(object sender, FriendsStatusItemsArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
txt_OnlineFriends.Text = "Online Friends";
ListBoxFriends.ItemsSource = e.ListFriendsStatusItems;
});
}
  • Call Connect() method to start a session which takes Facebook Access Token and Facebook Key as parameters.
App. iXFacebook.Connect(Global.GAccessToken, Global.FacebokKey);
  • Event OnLoadedMessageReceived is called when you receive message from user.
App.iXFacebook.OnLoadedMessageReceived += iXFacebook_OnLoadedMessageReceived;
 
void iXFacebook_OnLoadedMessageReceived(object sender, NewMessageItemsArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (e.iNewMessageItems.IsReceived)
{
MessagesViewModel viewModel = this.DataContext as MessagesViewModel;
CustomMessage customMessage = new CustomMessage(e.iNewMessageItems.Message, DateTime.Now.AddMinutes(1), ConversationViewMessageType.Incoming, viewModel.ConversationBuddy.PersonId);
viewModel.Messages.Add(customMessage);
}
});
}
  • SendMessage() method is used to send chat message, which takes user id and text message as parameter.
App.iXFacebook.SendMessage(originalMessage.Text, Global.GSelectedFriendId);
  • To get user conversation state changed call the event OnLoadedUserConversationStateChanged
App.iXFacebook.OnLoadedUserConversationStateChanged += iXFacebook_OnLoadedUserConversationStateChanged; 
 
void iXFacebook_OnLoadedUserConversationStateChanged(object sender, ConversationStateItemsArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
txt_ConversationState.Text = e.iConversationStateItems.State;
});
}
  • Before you run the application make sure to add your Facebook Key and Secret in Global.cs file in project folder.
  public const string FacebokKey = "<YourKey>";
public const string FacebokSecret = "<YourSecret>";

Usage of APIs used in this library

  • OnLoadedFriendList : Returns Facebook friend list
  • OnLoadedMessageReceived : Returns with Chat message from user
  • OnLoadedOnlineFriends : Returns list of online friends
  • OnLoadedUserConnected : Returns user connected status
  • OnLoadedUserConversationStateChanged : Returns user conversation state
  • OnLoadedUserDisconnected : Called when user is disconnected
  • OnLoadedUserStateChanged : Returns user session state changed
  • Connect() : Create a chat session
  • SendMessage() : Send message to user

Reference

During the course of the development of this library I went through many XMPP libraries for .Net and C++. Though I liked few but either those are paid or doesn’t support X-FACEBOOK Chat implementation. The most helpful for me was XMPP/Media library for .Net and Windows Phone 7.5 which saved much of my time implementing a complete XMPP stack.

Source Code

Know Issue

Sometimes you might come across to a situation where you don't see friend list on the screen. This is not because of any issue in the library but the application list box which doesn't update when item gets added in the list. For verification call the event OnLoadedOnlineFriends and see what you get in the response.

 void iXFacebook_OnLoadedOnlineFriends(object sender, FriendsStatusItemsArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
 
txt_OnlineFriends.Text = "Online Friends";
ListBoxFriends.ItemsSource = e.ListFriendsStatusItems;
 
});
}

Use ObservableCollection or some other way to get better notified when there are changes in the list items.

History

I started working on this library when I saw the DiBo post. And took it as a challenge after it got featured. There are lots many areas of improvement in the library, so if you have any suggestion or find bugs while using this library please keep coming your feedback in the comment section to help me update the library. We will come up with source code of the Library in our next release.

609 page views in the last 30 days.
×