Reliable and Fast Messaging and Notification Services Using Alibaba Cloud Message Service for Android

Image for post
Image for post

By Sai Sarath Chandra Alibaba Cloud Tech Share Author

This is a sample project created to demonstrate the usage of Alibaba Cloud Message Service on Android. This application shows the capability of the Message Service for messaging and notification services (MNS). You can find the repo of this project on my GitHub page.

Prerequisites:

  1. You need an Alibaba Cloud Account. If you need one, you can get one with $300 by signing up here.
  2. You’ll need Android Studio 3.0.0 to build the Android app.

Steps:

1.Activate the Alibaba Cloud Message Service & get the access keys from your Alibaba Cloud Console. You can find more information about this step on the documentation center.
2.Create Queue and get the endpoint. Follow this link for an illustrative guide.
3.Create a new Android project with Android Studio 3.0.0.

Image for post
Image for post

4.Select your required values. I am targeting API 19 and above.

Image for post
Image for post

5.Select the appropriate activity. I am choosing empty activity

Image for post
Image for post

6.Create the class, give an appropriate class name, and create the appropriate activity.xml too. I am using MainActivity.

Image for post
Image for post

7.Fill the XML details in below and copy it in the strings.xml.

8.Then, switch your project view to Project in your Android Studio as shown below:

Image for post
Image for post

9.Go to the libs subfolder located under app folder, and copy the two libraries — aliyun-sdk-mns-1.1.18.jar & Jaxb-api.2.2.12.jar. Right click the libs folder and you will see the option “Add to Library”.

Image for post
Image for post

10.Go to your app build.gradle file then copy the below

11.Enable multidex in your custom application class. Then, add this application class to AndroidManifest.xml

12.Then enable the application to access the internet by adding “uses-permission” outside application tag but inside manifest.

and then copy paste the below after your activity tag

13.Now, we have to create the service for our application, which receives notification, broadcast receiver, UI, and logic for sending, receiving, and sending notifications. 13. Let’s create the UI copy the below code to your activity_main.xml.

14.Copy the below code and add it to your MainActivity.java

Then Create a class named “ReceiveService” and cope the below code.

15.Then, create a class named MessageReceiverand copy the below code

16.Create a package asynctasks package, and under that create a class called RcvNtfcnTask and copy this code.

17.Then, create one more XML named inputdialog under res > layout, and copy the below code

18.That’s it! If everything is done correctly, all your compilation issues will go away and the application starts installing by clicking the small play (run) button in the status bar of Android Studio.

Image for post
Image for post

Please take a look at my GitHub page for the final code repo and let me know if you face any issues, or raise any pull requests for improvements!

Reference:

https://www.alibabacloud.com/blog/Reliable-and-Fast-Messaging-and-Notification-Services-Using-Alibaba-Cloud-Message-Service-for-Android_p279212?spm=a2c41.11187625.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store