How to Develop a Messaging App on Xamarin.Android by HUAWEI Push Kit
If you have not seen that article, you can see the link below. It also shows how to integrate Push Kit into your Xamarin.Android Application.
Join the DZone community and get the full member experience.Join For Free
In this article, I will explain how to develop a basic messaging app by Huawei Push Kit. If you want to develop a messaging app or add a messaging section into your application, I hope this article will be useful for you.
Before beginning, this application and article will be the addition of my first ones. If you have not seen that article, you can see the link below. It also shows how to integrate Push Kit into your Xamarin.Android Application.
Layout of Activity_Main
We will only add our messaging button to pass to the messaging section. The final version of “activity_main.xml” will be like this.
Firstly, we will inherit the ViewAnimator.IOnClickListener interface to this class. Then, we will organize inside of OnCreate Method and implement the OnClick method.
Secondly, we will arrange the OnClick method so that we can call these events from one method.
Thirdly, as you guess we will add the StartMessagingMain method to call MessagingMainActivity class. The calling StartActivity method is adequate here. We don’t have to send any Intent.
Finally, we will move AccessToken, SendDataMessage, and SendNotification method to the new HmsPushKit class.
We should be able to call these methods from any other class. Because of that, methods in this class should be static.
Actually, the database is not essential for a sample application but I want to make it pretty. I will use SQLite-net-PCL by SQLite-net.
This application will need two essential database tables(Model) Person and Message. We will also use the UniqueData table for minor data.
And the Database class that I use is below. The most part is easy to understand. If you want to use another Model you should add this into the CreateDataBase method. Moreover, you don’t have to check if there is a database. If the database does not exist, it will create otherwise it will not.
Layout of Activity_main_messaging
I will share with you a simple screen for the list of people.
My xml file will not contain the rows. We will add them from MessagingMainActivity class. Xml code for this layout below.
Things we will do in this class are bringing people from the database, adding new person, deleting person, and starting messaging. So let’s start with identifying Delete Button, New button, and LinearLayout which is in the ScrollView.
After we will set ClickListener on two buttons, therefore, we will inherit ViewAnimator.IOnClickListener to this activity as well.
Later, we need to take an instance of our DataBase class then add people into our LinearLayout. Furthermore, I will also add sample data.
At last, we will fill the OnClick method.
As you see there is the OneRowLL method. It returns a linearlayout which contains CircleImageView from Refactored.Controls.CircleImage NuGet and linearlayout contain two textviews.
Afterward, we will create the AddPerson method for btnNew. We will use LayoutInflater so I also create an xml file as well. We need the user’s code to get his token and a name to display it.
The next step is btnDel. The delete button will be simple. Click delete then click person afterward person is gone. That’s adequate for now but you can improve it by adding an invisible red delete button to each row and ask for approval.
Finally, LinearLayoutClick for a pass to messaging. We will send their code by Intent.
Layout of activity_messaging.xml
There will be 3 sections. The first one is the LinearLayout which shows who we message. The second one is ScrollView for messages. And the last one is EditText for typing and sending. Xml file below.
This class is the most complicated one in this application. We will go step by step in the OnCreate method.
1. Define variables and views.
2. Get Person by using the code which we sent from MessagingMainActivity.
friendsCode = Intent.Extras.GetString(
friend = db.SelectTable<Person>().Where(x => x.Code == friendsCode).First();
3. Identify views we will use and add txtInput’s events.
imageIcon = FindViewById<CircleImageView>(Resource.Id.imageIcon);
txtName = FindViewById<TextView>(Resource.Id.txtName);
linearLayout = FindViewById<LinearLayout>(Resource.Id.linearLayout);
txtStatus = FindViewById<TextView>(Resource.Id.txtStatus);
scrollView = FindViewById<ScrollView>(Resource.Id.scrollView);
txtInput = FindViewById<TextInputEditText>(Resource.Id.textInput);
txtInput.KeyPress += CatchPress;
txtInput.TextChanged += TextChanged;
4. Add image to ImageView and friend’s name to TextView
txtName.Text = friend.Name;
id = Resources.GetIdentifier(
5. Bring previous messages.
We added the CatchPress method to txtInput. We will send messages when the user clicks the Enter button.
Furthermore, there is also a TextChanged event. The reason I add this event is I want to show users if their friend is typing.
Finally, the most important method AddMessage. The most vital part of this activity is adding a message to LinearLayout which is in ScrollView. There are three parameters. “msg” is the message we add. “code” is senders code. “isNew” stands for if it is new which means if you want to insert to database.
Last but not least we should complete OnReceive method in MyBroadcastReceiver. If you know how data message works, you will figure this method out easily. When data messages come, the OnMessageReceive method is triggered. We send this data message by SendBroadCast method and calling this method triggers OnReceive method. If type is status we change the status base on the message. If it is a message we use the AddMessage method from MessagingActivity.
In conclusion, it was a basic messaging app easy to use on different apps. My aim is not just to develop a messaging app also to show how fast Huawei Push Kit works. I hope, it will be useful.
Opinions expressed by DZone contributors are their own.