Android Push Notification Tutorial

I cam across this project recently, The Deacon Project. This project aims to bring the push notifications to android more platform more easily. I've tested the deacon project and I liked it. It is very easy to use.

Requirements

  • The deacon library: You can download it from here: http://deacon.daverea.com I downloaded version 0.2.0 beta.
  • Meteor server: Meteor is an open source http server, the meteor server will be used as push server. Can be downloaded from here: http://www.meteorserver.org/
  • Eclipse with ADT plugin.
Deacon - Quick How To
 
 

I recommend you watch this tutorial (By David Rea - on of the contributor of the deacon project) before you continue with this article. It will help you understand deacon project more easy.

Note: I am working under Ubuntu Linux, so the some of the instruction here are for Linux machine.I installed Meteor server on VM machine (also Ubuntu) and my android app is running on my local host.

Step 1: Meteor installation and configuration

  • Create a new directory on your machine for the server, let's call it "meteor".
  • If you have not downloaded Meteor yet,  open a terminal and execute the following command:
wget http://meteorserver.org/download/latest.tgz
  • Copy the sever compressed file (latest.tgz) to the newly created "meteor" directory.
  • Extract the compress file.
  • Open a file called "daemonctroller.dist" with your favorite editor
  • At line 12 change the directory for meteor server according to your installation. I installed it in /home/khalid/meteor so the line should be like this
cd /home/khalid/meteor
  • Change the user who is going to run the server. The default user is in the daemonctroller file is "meteor". You can create this user if you don't want to change the user
sudo -u khalid
  • Save the changes and close the file.
  • Copy this file (daemonctroller.dist) to /etc/init.d/meteord
sudo cp daemoncontroller.dist /etc/init.d/meteord
  • Change the permissions for this file
sudo chmod +x meteord
  • Next, copy the configuration file (meteord.conf.dist) to /etc/meteord.conf.
sudo cp meteord.conf.dist /etc/meteord.conf
  • start the server
./meteord -d

The -d option is to run the server in debug mode. After the server is started, you can test if everything working okay or no. To test your installation, please refer to the installation guide.

Step 2: Using the Deacon Library.
  • If you was too lazy to watch the youtube tutorial at the beginning of this article, here is the code from that tutorial
public class TestingDeacon extends Activity implements DeaconObserver{
    private Deacon deacon;
    private TextView textBos;       
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        this.textBos = (TextView) findViewById(R.id.pushArea);        
        try{
            this.deacon = new Deacon("home.daverea.com", 4670, this);
            deacon.joinChannel("2sec", 0);
            deacon.catchUpTimeOut(60);         
            deacon.register(this);         
            deacon.start();        
        }catch (Exception e) {
            e.printStackTrace();        
        }
    }
 
    @Override
    public void onError(DeaconError err) {}
 
    @Override  
    public void onPush(DeaconResponse response) {      
        String currentText = (String) this.textBos.getText();                
        Calendar now = Calendar.getInstance();     
        this.textBos.setText("Push@"+String.format("%02d", now.get(Calendar.HOUR_OF_DAY)) + ":" +
            String.format("%02d", now.get(Calendar.MINUTE)) +", Channel = " + response.getChannel() +
            ", Payload = " + response.getPayload(); + "\n" + currentText);     
    }
 
    @Override  
    public void onReconnect() {}
  • We will need to change some parts of this code. Namely, the initialization of the Deacon object and the channel we are going to subscribe to. As I mentioned earlier, my meteor server is running on a VM on my local machine. I changed the initialization part setting the host name to my test machine's IP address like this:
this.deacon = new Deacon("172.16.15.147", 4670, this);
  • Next we need to change the channel name like this:
deacon.joinChannel("mychannel", 0);
  • Note that we did not create "mychannel" yet. We will do that in the next step.
  • Run the app and keep it running. There should be nothing displaying on the app but on the meteor server log, you should see a message for a new subscription notification.
Step 3: Creating the channel and send push notification
  • We need to connect to meteor controller. The controller is running by default on port number 4671. You can use any telnet/ssh client. I am using Putty .
telnet server-ip 4671
  • Once you are connected, execute this command.
ADDMESSAGE mychannel Hello Notifications world!
  • If you see the following message on your controller screen, this mean the message was successfully sent.
OK 1
  • The number next to the (Ok) string indicates the number of messages. Since this is our first message the number is 1. This command will add message to a channel. If the channel does not exist it will be created. The syntax of the command is
ADDMESSAGE CHANNEL_NAME YOUR_MESSAGE
  • Confirm that you can see the message on Android Application. It is pretty simple, right :)
[Extra] Creating notification on the status bar
 
Note: You can add the following code inside the onPush method or create new method and called from the onPush method.
  • First step is to get a reference to the notification manager
String notificaionService = NOTIFICATION_SERVICE;
NotificaionManager notifManager = (NotificaionManager) this.getSystemService(notificaionService);
  • Then we will need to create an instance of the Notification class and initialize it.
CharSquence tickerText = "Notificaion";
long timeReceived = System.currentTimeMillis();
int icon = R.drawable.not_icon  //Note you need to create this icon your self. or use the defult icon
Notification notification = new Notification(icon,tickerText,timeReceived);
  • Next we need to define expanded message and its intent.
Context context = getApplicaionContext();
CharSquence contentTitle = "New Message Received";
CharSquence contentMessage = response.getPayload();
Intent notificaionIntent = new Intent(getApplicaionContext(),TestingDeacon.Class);
PendingIntent contentIntent = PindingIntent.getActivity(getApplicaionContext(),0,notificaionIntent,0);
notification.setLastEventInfo(context,contentTitle,contentMessage,contentIntent);
  • Finally, pass the notification to the notification manager.
notifManager.notify(1,notification);

That is it. Try pushing another message and this now you will see some notification on the status bar. I hope this tutorial was useful for you. Enjoy the Deacon project

Comments

You must be logged in to post comments.