Tuesday 25 August 2015

Different Types of Notifications in Android

You may have heard about Android Jelly Bean (API level 16). Google has improved a lot of features and introduced new features. One of them is the notification. Now they have made the notification more versatile by introducing media rich notification. Google has come up with three special style of notification which are mentioned below. Even developer can write his own customized notification style using remote view.The old Notification class constructor has been deprecated and a brand new and enhanced version of Notification has been introduced.

Notification Type:

  • Basic Notification – Shows simple and short notification with icon.
  • Big Picture Notification – Shows visual content such as bitmap.
  • Big Text Notification – Shows multiline Textview object.
  • Inbox Style Notification – Shows any kind of list, e.g messages, headline etc. 
Old syntax requires us to create an object of notification but now Android uses builder patter to create the notification object. Notification.Builder class has been introduced to make this task easier. This class returns the builder object which is configurable according to your requirements. The helper classes have been introduced like Notification.BigPictureStyle, Notification.BigTextStyle, and Notification.InboxStyle. These classes are re-builder classes which take object created by Notification.Builder class and  modify the behavior like so.

Activity_Notification.XML

<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"
    android:gravity="center_horizontal">

    <Button
        android:id="@+id/btBasicNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="sendBasicNotification"
        android:text="BasicNotification" 
        
        android:textColor="#000000"
        />
    <Button
        android:id="@+id/btBigTextNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="sendBigTextStyleNotification"
        android:text="BigTextNotification" 
        
        android:textColor="#000000"
        />
   <Button
        android:id="@+id/btBigPictureNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="sendBigPictureStyleNotification"
        android:text="BigPictureNotification"
        
        android:textColor="#000000" />
   <Button
        android:id="@+id/btInboxStyleNotification"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|center_vertical"
        android:onClick="sendInboxStyleNotification"
        android:text="InboxStyleNotification"
        
        android:textColor="#000000"/>
</LinearLayout>


NotificationActivity.Java

package com.example.notifications;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;


@SuppressLint("NewApi")
public class NotificationActivity extends Activity {


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

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public void sendBasicNotification(View view) {
 Notification notification = new Notification.Builder(this)
   .setContentTitle("Basic Notification")
   .setContentText("Basic Notification, used earlier")
   .setSmallIcon(R.drawable.ic_launcher).build();
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
 NotificationManager notificationManager = getNotificationManager();
 notificationManager.notify(0, notification);
}

public void sendBigTextStyleNotification(View view) {
 String msgText = "Jeally Bean Notification example!! "
   + "where you find three different kind of notification. "
   + "will see here different types of notifications";

 NotificationManager notificationManager = getNotificationManager();
 PendingIntent pi = getPendingIntent();
 android.app.Notification.Builder builder = new Notification.Builder(this);
 builder.setContentTitle("Big text Notofication")
   .setContentText("Big text Notification")
   .setSmallIcon(R.drawable.ic_launcher)
   //.setAutoCancel(true);
   .setPriority(Notification.PRIORITY_HIGH)
   .addAction(R.drawable.ic_launcher, "show activity", pi);
 Notification notification = new Notification.BigTextStyle(builder)
   .bigText(msgText).build();
 
 notificationManager.notify(0, notification);
}

public void sendBigPictureStyleNotification(View view) {
 PendingIntent pi = getPendingIntent();
 android.app.Notification.Builder builder = new Notification.Builder(this);
 builder.setContentTitle("BP notification")
   // Notification title
   .setContentText("BigPicutre notification")
   // you can put subject line.
   .setSmallIcon(R.drawable.ic_launcher)
   // Set your notification icon here.
   .addAction(R.drawable.ic_launcher, "show activity", pi)
   .addAction(
     R.drawable.ic_launcher,
     "Share",
     PendingIntent.getActivity(getApplicationContext(), 0,
       getIntent(), 0, null));

 // Now create the Big picture notification.
 Notification notification = new Notification.BigPictureStyle(builder)
   .bigPicture(
     BitmapFactory.decodeResource(getResources(),
       R.drawable.ic_launcher)).build();
 // Put the auto cancel notification flag
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
 NotificationManager notificationManager = getNotificationManager();
 notificationManager.notify(0, notification);
}

public void sendInboxStyleNotification(View view) {
 PendingIntent pi = getPendingIntent();
 android.app.Notification.Builder builder = new Notification.Builder(this)
   .setContentTitle("IS Notification")
   .setContentText("Inbox Style notification!!")
   .setSmallIcon(R.drawable.ic_launcher)
   .addAction(R.drawable.ic_launcher, "show activity", pi);

 Notification notification = new Notification.InboxStyle(builder)
   .addLine("First message").addLine("Second message")
   .addLine("Thrid message").addLine("Fourth Message")
   .setSummaryText("+2 more").build();
 // Put the auto cancel notification flag
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
 NotificationManager notificationManager = getNotificationManager();
 notificationManager.notify(0, notification);
}

public PendingIntent getPendingIntent() {
 return PendingIntent.getActivity(this, 0, new Intent(this,
   HandleNotificationActivity.class), 0);
}

public NotificationManager getNotificationManager() {
 return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
}
raj.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical" >
android:layout_height="match_parent"
</LinearLayout>


HandleNotificationActivity.Java

package com.example.notifications;
import android.app.Activity;
public class HandleNotificationActivity extends Activity{
import android.os.Bundle; @Override
// TODO Auto-generated method stub
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
}
setContentView(R.layout.raj); }









No comments:

Post a Comment

Ads Inside Post