Tutorial details

Android beginner tutorial: Part 62 - Notification | App Code for Sale | Preview

Android beginner tutorial: Part 62 - Notification | Android Tutorial

How to use a system service to create Notifications

Overview PAGE TOP

Today we'll learn how to use a system service to create Notifications.

A Notification is a note that appears in the top of the screen, has an icon, a title and a descrition text.

Go to the activity_main.xml layout and create 2 buttons - one to start a notification and one to update it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button android:id="@+id/notifyButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start notification"
        />

    <Button android:id="@+id/updateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update notification"
        />

</LinearLayout>

Go to MainActivity class and declare 4 variables - an identificator for the notification, a NotificationManager instance, a NotificationCompay.Builder instance and an Intent instance.

private static final int NOTIFY_ID = 101;
private NotificationManager notificationManager;
private NotificationCompat.Builder nbuilder;
private Intent resultIntent;

In the onCreate() function, set notificationManager to refer to a system service that manages notifications. Do that using the getSystemService() method as shown below:

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Now set the nbuilder to a new NotificationCompat.Builder() object. This will let us build our notification later:

nbuilder = new NotificationCompat.Builder(MainActivity.this);

Now set the Intent instance to an Intent that would be used when the user taps the Notification. I will direct it to a SecondActivity class:

resultIntent = new Intent(MainActivity.this, SecondActivity.class);

Now add a click listener to the notifyButton button. In the onClick() function, set the icon, title and text of our notification using nbuilder's setSmallIcon(), setContentTitle() and setContentText() methods.

After that, we declare a TaskStackBuilder instance. Basically it is a Stack that we artificially create in order to get a PendingIntent instance, which is then passed to the builder via its setContentIntent() method.

Then we use the notify() method of the notificationManager object to show the notification.

final Button notifybtn = (Button)findViewById(R.id.notifyButton);
notifybtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
nbuilder.setSmallIcon(R.drawable.snowflake);
nbuilder.setContentTitle("Notification!");
nbuilder.setContentText("Hello world!");

TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(SecondActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

nbuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFY_ID, nbuilder.build());
}
});

To update the notification, do whatever you need with the builder and then call the notify() function again. It is important to keep the same ID in order to update an existing notification:

final Button updatebtn = (Button)findViewById(R.id.updateButton);
updatebtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
nbuilder.setContentText("Updated message at " + new Date().toString());
notificationManager.notify(NOTIFY_ID, nbuilder.build());
}
});

Full code:

package com.kircode.codeforfood_test;

import java.util.Date;

import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity{

private static final int NOTIFY_ID = 101;
private NotificationManager notificationManager;
private NotificationCompat.Builder nbuilder;
private Intent resultIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nbuilder = new NotificationCompat.Builder(MainActivity.this);
resultIntent = new Intent(MainActivity.this, SecondActivity.class);

final Button notifybtn = (Button)findViewById(R.id.notifyButton);
notifybtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
nbuilder.setSmallIcon(R.drawable.snowflake);
nbuilder.setContentTitle("Notification!");
nbuilder.setContentText("Hello world!");

TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(SecondActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

nbuilder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFY_ID, nbuilder.build());
}
});

final Button updatebtn = (Button)findViewById(R.id.updateButton);
updatebtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
nbuilder.setContentText("Updated message at " + new Date().toString());
notificationManager.notify(NOTIFY_ID, nbuilder.build());
}
});
}

}

Now let's go to the second activity. Open it's layout file activity_second.xml, add a button that kills the notification:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:text="Second Activity"
        />

    <Button android:id="@+id/killButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Kill notification"
        />

</LinearLayout>

In the SecondActivity.java class add a click listener that removes the notification using the cancel() method of the notification manager:

package com.kircode.codeforfood_test;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity{

private static final int NOTIFY_ID = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

final Button killbtn = (Button)findViewById(R.id.killButton);
killbtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFY_ID);
}
});
}

}

It is also possible to cancel all the notifications you've called using cancelAll().

You can also use setAutoCancel() method of the NotificationCompat.Builder object to make the notification kill itself when it's selected.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-62.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.