Home  >  Android

Android Notification Example with Vibration, Sound, Action and Big View Styles

By Arvind Rai, June 19, 2015
In this page we will walk through android notification example with vibration, sound, action and big view styles. Android provides NotificationManager which provides notify method to display notification. On click of notification, we can define action and for that we have to create PendingIntent. To create notification layout, we use NotificationCompat.Builder which has different method to enable vibration, sound, action button, big view style etc. Find the example step by step.

Create PendingIntent for Notification Click Action

To create notification, first step is to create PendingIntent.
Intent myIntent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
On click of notification, we set an action. In our case we are displaying an activity. Create an activity class. Here we have created NotificationActivity. Using it instantiate Intent and then instantiate PendingIntent.

Create NotificationCompat.Builder

android.support.v4.app.NotificationCompat.Builder creates notification layout. It provides methods using which we configure notification with sound, vibration, add action button, big view etc.
NotificationCompat.Builder  notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
        .setContentTitle("Notification Demo").setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent)
        .setContentText("You have got notification.");	 

Create NotificationManager and notify Method

android.app.NotificationManager notifies notification to the user using notify method. The notification can be of different variety.
1. Persistent icon in status bar.
2. Flashing led on device.
3. Playing sound
4. By vibration
Every notification has an id. If we notify notification with same id, the previous one will be overridden.
NotificationManager notificationManager = notificationManager.notify(1, notificationBuilder.build());
 
Here notification id is 1.

Notification with Vibration

To get notification with vibration, NotificationCompat.Builder provides setVibrate method in which we have to pass long array of milliseconds in int to turn on or off the vibrator.
long[] v = {500,1000};
notificationBuilder.setVibrate(v);
The first index of array is number of milliseconds to wait before turning the vibrator on and the second index is number of millisecond to vibrate before turning it off. Do not forget to allow VIBRATE permission in AndroidManifest.xml.

Notification with Sound

To get sound in notification, first get the notification sound URL by using RingtoneManager.TYPE_NOTIFICATION then call setSound method.
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder.setSound(uri); 

Notification with Big View Styles

Big view style in notification only works when the platform supports rich notification styles. We call setStyle method to enable it as follows.
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText("Big View Styles"));

Notification with Action Button

Before Android 4.1, action button will not appear. To enable action button NotificationCompat.Builder provides addAction (int icon, CharSequence title, PendingIntent intent) method. Here intent is created to get action on click of action button. We can add more than one action button.
PendingIntent childPIntent = PendingIntent.getActivity(MainActivity.this, 0, childIntent, 0);
notificationBuilder.addAction(R.drawable.ic_launcher, "Add", childPIntent); 

Complete Example

Now find a complete example in which we are creating notification with sound and vibration.

Create Activity Classes


MainActivity.java
package com.concretepage.android;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity{
	private PendingIntent pendingIntent;
	private NotificationManager notificationManager;
	private NotificationCompat.Builder notificationBuilder;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
	//create Intent
        Intent myIntent = new Intent(this, NotificationActivity.class);
        //Initialize PendingIntent
        pendingIntent = PendingIntent.getActivity(this, 0, myIntent, 0);
        //Initialize NotificationManager using Context.NOTIFICATION_SERVICE
        notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
         
        //Create listener to listen button click
        OnClickListener listener = new OnClickListener() {
 			public void onClick(View view) {
		        //Prepare Notification Builder
		        notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
        		.setContentTitle("Notification Demo").setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent)
        		.setContentText("You have got notification.");	 
		    	//add sound
		        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			    notificationBuilder.setSound(uri);
			    //vibrate
			    long[] v = {500,1000};
			    notificationBuilder.setVibrate(v);
			    notificationManager.notify(1, notificationBuilder.build());
 			}
          }; 
         Button btn = (Button)findViewById(R.id.button);
         btn.setOnClickListener(listener);
    }
}  
NotificationActivity.java
package com.concretepage.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NotificationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.msglayout);
        TextView textView = (TextView)findViewById(R.id.msg);
		textView.setText("Hello World!");
    }
} 
ChildNotificationActivity.java
package com.concretepage.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ChildNotificationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.child_msg_layout);
        TextView textView = (TextView)findViewById(R.id.childmsg);
	textView.setText("Child World!");
    }
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.concretepage.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <application
        android:allowBackup ="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NotificationActivity" android:label="Notification Click Action"/>
        <activity android:name=".ChildNotificationActivity" android:label="Child Notification Click Action"/>         
    </application>
</manifest> 

Layout XMLs

mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#D1C4E9"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn"/>
</LinearLayout> 
msglayout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#D1C4E9"
    android:orientation="vertical" >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
	    android:layout_gravity="center"
	    android:textColor="#b0b0ff"
	    android:textSize="30sp"
	    android:id="@+id/msg"/>
</LinearLayout> 
child_msg_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="#D1C4E9"
    android:orientation="vertical" >
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
	    android:layout_gravity="center"
	    android:textColor="#b0b0ff"
	    android:textSize="30sp"
	    android:id="@+id/childmsg"/>
</LinearLayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Concrete Page</string>
    <string name="btn"> Notification Demo</string>
</resources> 

Output

Android Notification Example with Vibration, Sound, Action and Big View Styles

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2018 concretepage.com | Privacy Policy | Contact Us