Android Notification ++

Hi friend,

Today, I was working in creating the Notification. I have to notify, when back button is pressed from my android app. As usual I went through the android developer site and studied how to do notify in the notification bar. But I had a small problem in it, which you can also get, when you work in Android Notification. So I planned to share it here.

Android Notification ++

Creating Notification is very simple. Just read it in android developer portal, probably you have to end with the following lines of code to create notification.

/Overriding the Back Button press event /
@Override
    public void onBackPressed() {
    // do something on back.

/* Get a reference to the NotificationManager */
    NotificationManager notificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);

/* Instantiate the Notification */
    Notification notification = new Notification(R.drawable.icon,
            "A new notification", System.currentTimeMillis());

/* Hide the notification after its selected */
    notification.flags |= Notification.FLAG_AUTO_CANCEL;


/*  Creating the intent to an activity, which has to be called and setting it to notification */
    Intent intent = new Intent(this, ActivityName.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);

/*Pass the Notification to the NotificationManager */   
    notification.number += 1;
    notificationManager.notify(0, notification);
    }

Sending the activity to background

But above lines of code will not send the activity to the background. So you will see the notification but the activity will be open. So you have to add the following line to send the activity to the background.

moveTaskToBack (true);

Bringing the activity to front


Once the notification is clicked, the intent will be created newly (note that the activity will be created newly). Older activity will be running in the back of the newly created activity. But most of the case we don't need it. We will need to open the activity that we sent to the background. The following line will do that magic for us.

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

NOW

Creating the notification and sending the task to the back and on clicking the notification bringing the same old activity (or task) to the front is done by the following lines of code.

 @Override
    public void onBackPressed() {
        // do something on back.
        moveTaskToBack (true);
        NotificationManager notificationManager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    Intent intent = new Intent(this, Test.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "This is the title",
            "This is the text", activity);
    notification.number += 1;
    notificationManager.notify(0, notification);
    }

 Thats All!!! enjoy!!! 

Newer Posts Older Posts Home

About Me

My photo
Hi everyone,myself Alagappan...electronic and communication engg. student... living in madurai... interested in everything... want to achieve something great in my lifetime...

Followers


Recent Comments