The notification system allows your app to keep the user informed about events, such as new chat message, playing song. Think of notifications as a news channel that alerts the user to important events as they happen while user is not paying attention.
Import andorid-support-v4.jar in your project first.
Project Hierarchy looks like this-
1) Simple Notification
2) Two icon Notification
3) Big Picture Style Notification
4) Big Text Style Notification
5) Chat Style Notification
6) Action Button Notification
8) Simple Custom Notification
9) Expanded Custom Notification
NOTE:- Custom big view requires minimum API 16.
Full code snippet-
Project Hierarchy looks like this-
Project Hierarchy |
public static void
simpleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Simple
Notification");
nc.setContentText("Showing
notification content");
nc.setTicker("Ticker
1");
nc.setNumber(2);
nm.notify(1,
nc.build());
}
|
2) Two icon Notification
public static void
twoIconNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Two Icon
Notification");
nc.setContentText("Showing
two icon content");
nc.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_action_person));
nc.setTicker("Ticker
2");
nc.setNumber(3);
nm.notify(2,
nc.build());
}
|
3) Big Picture Style Notification
public static void
bigPictureNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigPictureStyle
bigPictureNotification = new NotificationCompat.BigPictureStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Big
Picture Style");
nc.setContentText("Big
Picture Style Content");
nc.addAction(R.drawable.ic_action_share, "Share", null);
bigPictureNotification.bigPicture(BitmapFactory.decodeResource(context.getResources(),
R.drawable.big_img));
bigPictureNotification.setBigContentTitle("New Big
Picture Title");
nc.setStyle(bigPictureNotification);
nm.notify(3,
nc.build());
}
|
4) Big Text Style Notification
public static void
bigTextStyleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigTextStyle
bigTextNotification = new NotificationCompat.BigTextStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Big Text
Style");
nc.setContentText("Big Text
Style Content");
bigTextNotification.setBigContentTitle("New Big
Text Title");
bigTextNotification.bigText("Starting
with Jelly Bean, Android supports optional actions that are displayed at the
bottom of the notification. With actions, users can handle the most common
tasks for a particular notification from within the notification shade without
having to open the originating application. This speeds up interaction and, in
conjunction with swipe-to-dismiss, helps users to streamline their notification
triaging experience.");
bigTextNotification.setSummaryText("By:
http://developer.android.com/");
nc.setStyle(bigTextNotification);
nm.notify(4,
nc.build());
}
|
5) Chat Style Notification
public static void
chatStyleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.InboxStyle
inboxStyle = new NotificationCompat.InboxStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String[]
lines = new String[7];
lines[0]
= "David@
Hi Howard";
lines[1]
= "Howard@
Hi David";
lines[2]
= "David@
What is the agenda for the upcoming meeting?";
lines[3]
= "Howard@
I will send you a copy";
lines[4]
= "David@
Thank you";
lines[5]
= "Howard@
I will talk to you later";
lines[6]
= "David@
Bye for now";
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Chat
Text");
nc.setContentText(lines[0]);
nc.setNumber(7);
for (int i = 0; i <
lines.length; i++) {
inboxStyle.addLine(lines[i]);
}
inboxStyle.setBigContentTitle("New Chat
Title");
inboxStyle.setSummaryText("7
messages from 2 conversations.");
nc.setStyle(inboxStyle);
nm.notify(5,
nc.build());
}
|
6) Action Button Notification
public static void
actionButtonNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigTextStyle
bigTextNotification = new NotificationCompat.BigTextStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_end_call);
nc.setAutoCancel(true);
nc.setContentTitle("Big Text
Style Button");
nc.setContentText("(900)022-3365");
nc.addAction(R.drawable.ic_action_call, "Call
back", null);
nc.addAction(R.drawable.ic_action_chat, "Message", null);
bigTextNotification.setBigContentTitle("New Big
Text Title");
bigTextNotification.bigText("Missed
call from (900)022-3365");
nc.setStyle(bigTextNotification);
nm.notify(6,
nc.build());
}
|
7) Open Activity On Notification Click
public static void
openActivityNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Creates an
Intent for the Activity
Intent notifyIntent = new Intent(context,
MainActivity.class);
// Sets the
Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// Creates the
PendingIntent
PendingIntent
pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Click to
open Activity");
nc.setContentText("Click
please");
nm.notify(9,
nc.build());
}
|
Custom Notification and call Broadcast Receiver
8) Simple Custom Notification
- Make custom_notification.xml file in layout folder, and copy below code
custom_notification.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/album_art" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp"
>
<TextView
android:id="@+id/textSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/textAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#C0C0C0"
android:textSize="15dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="@+id/btnPause"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_pause"
/>
<Button
android:id="@+id/btnPlay"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_play"
android:visibility="gone"
/>
<Button
android:id="@+id/btnNext"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_fast_forward"
/>
</LinearLayout>
<Button
android:id="@+id/btnDelete"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:background="@drawable/ic_action_remove"
/>
</LinearLayout>
</LinearLayout>
|
- Now call this function which will show custom notification.
static String songName = "Now
You're Gone";
static
String albumName = "Now
Youre Gone - The Album";
public static void
customSimpleNotification(Context context) {
RemoteViews
simpleView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
Notification
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Custom
Big View").build();
notification.flags |=
Notification.FLAG_AUTO_CANCEL;
notification.contentView = simpleView;
notification.contentView.setTextViewText(R.id.textSongName, songName);
notification.contentView.setTextViewText(R.id.textAlbumName, albumName);
setListeners(simpleView,
context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(7,
notification);
}
|
- We want to perform some action on these buttons click so we will register Broadcast Receiver on the click. setListeners() sets the pending intent.
public static final String NOTIFY_PREVIOUS = "com.tutorialsface.notificationdemo.previous";
public static final String NOTIFY_DELETE = "com.tutorialsface.notificationdemo.delete";
public static final String NOTIFY_PAUSE = "com.tutorialsface.notificationdemo.pause";
public static final String NOTIFY_PLAY = "com.tutorialsface.notificationdemo.play";
public static final String NOTIFY_NEXT = "com.tutorialsface.notificationdemo.next";
private static void
setListeners(RemoteViews view, Context context) {
Intent
previous = new Intent(NOTIFY_PREVIOUS);
Intent
delete = new Intent(NOTIFY_DELETE);
Intent
pause = new Intent(NOTIFY_PAUSE);
Intent
next = new Intent(NOTIFY_NEXT);
Intent
play = new Intent(NOTIFY_PLAY);
PendingIntent
pPrevious = PendingIntent.getBroadcast(context, 0, previous,
PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent
pDelete = PendingIntent.getBroadcast(context, 0, delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent
pPause = PendingIntent.getBroadcast(context, 0, pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent
pNext = PendingIntent.getBroadcast(context, 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
PendingIntent
pPlay = PendingIntent.getBroadcast(context, 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
}
|
- Create NotificationBroadcast.java and define the broadcast receiver-
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.widget.Toast;
public class
NotificationBroadcast extends BroadcastReceiver {
public static final String NOTIFY_PREVIOUS = "com.tutorialsface.notificationdemo.previous";
public static final String NOTIFY_DELETE = "com.tutorialsface.notificationdemo.delete";
public static final String NOTIFY_PAUSE = "com.tutorialsface.notificationdemo.pause";
public static final String NOTIFY_PLAY = "com.tutorialsface.notificationdemo.play";
public static final String NOTIFY_NEXT = "com.tutorialsface.notificationdemo.next";
@Override
public void
onReceive(Context context, Intent intent) {
if
(intent.getAction().equals(NOTIFY_PLAY)) {
Toast.makeText(context,
"NOTIFY_PLAY", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NOTIFY_PAUSE)) {
Toast.makeText(context,
"NOTIFY_PAUSE", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NOTIFY_NEXT)) {
Toast.makeText(context,
"NOTIFY_NEXT", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NOTIFY_DELETE)) {
Toast.makeText(context,
"NOTIFY_DELETE", Toast.LENGTH_LONG).show();
}else if
(intent.getAction().equals(NOTIFY_PREVIOUS)) {
Toast.makeText(context,
"NOTIFY_PREVIOUS", Toast.LENGTH_LONG).show();
}
}
}
|
- Register NotificationBroadcast in manifest file.
<receiver android:name=".NotificationBroadcast"
>
<intent-filter>
<action android:name="com.tutorialsface.notificationdemo.previous"/>
<action android:name="com.tutorialsface.notificationdemo.delete"
/>
<action android:name="com.tutorialsface.notificationdemo.pause"
/>
<action android:name="com.tutorialsface.notificationdemo.next"
/>
<action android:name="com.tutorialsface.notificationdemo.play"
/>
</intent-filter>
</receiver>
|
9) Expanded Custom Notification
- Make big_notification.xml file in layout folder, and copy below code
big_notification.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/album_art" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@android:color/white"
android:textSize="20dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnDelete"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="5dp"
android:background="@drawable/ic_action_remove"
/>
</LinearLayout>
<TextView
android:id="@+id/textAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="#C0C0C0"
android:textSize="15dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:divider="@drawable/mydivider"
android:dividerPadding="12.0dip"
android:gravity="center"
android:orientation="horizontal"
android:showDividers="middle"
>
<Button
android:id="@+id/btnPrevious"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_fast_backward"
/>
<Button
android:id="@+id/btnPause"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_pause"
/>
<Button
android:id="@+id/btnPlay"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_play"
android:visibility="gone"
/>
<Button
android:id="@+id/btnNext"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="5dp"
android:background="@drawable/ic_action_fast_forward"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
|
- Make mydivider.xml in drawable folder.
my_divider.xml
<?xml version="1.0"
encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="1dip"
/>
<solid android:color="#ffffff"
/>
</shape>
|
- Now call this function which will show custom notification.
static String songName = "Now You're Gone";
static String albumName = "Now Youre Gone - The Album";
@SuppressLint("NewApi")
public static void
customBigNotification(Context context) {
RemoteViews
expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
Notification
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Custom
Big View").build();
notification.flags |=
Notification.FLAG_AUTO_CANCEL;
notification.bigContentView = expandedView;
notification.bigContentView.setTextViewText(R.id.textSongName, songName);
notification.bigContentView.setTextViewText(R.id.textAlbumName, albumName);
setListeners(expandedView,
context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(8,
notification);
}
|
NOTE:- Custom big view requires minimum API 16.
Full code snippet-
- activity_main.xml
<?xml version="1.0"
encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/btnNotification1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/simple_notification" />
<Button
android:id="@+id/btnNotification2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/open_activity" />
<Button
android:id="@+id/btnNotification3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/two_icon" />
<Button
android:id="@+id/btnNotification4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/big_picture" />
<Button
android:id="@+id/btnNotification5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/big_text" />
<Button
android:id="@+id/btnNotification6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/chat_text" />
<Button
android:id="@+id/btnNotification7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/big_text_button" />
<Button
android:id="@+id/btnNotification8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/custom_simple" />
<Button
android:id="@+id/btnNotification9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/custom_big" />
</LinearLayout>
</ScrollView>
|
- strings.xml
<resources>
<string name="app_name">NotificationDemo</string>
<string name="simple_notification">Simple</string>
<string name="open_activity">Open Activity</string>
<string name="two_icon">Two Icon</string>
<string name="big_picture">Big picture</string>
<string name="big_text">Big text</string>
<string name="chat_text">Chat text</string>
<string name="big_text_button">Big Text with Button</string>
<string name="custom_simple">Custom Simple</string>
<string name="custom_big">Custom Big</string>
</resources>
|
- MainActivity.java
MainActivity.java
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
public class MainActivity extends Activity{
Button
btnNotification1, btnNotification2, btnNotification3, btnNotification4, btnNotification5,
btnNotification6, btnNotification7, btnNotification8, btnNotification9;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNotification1 = (Button)
findViewById(R.id.btnNotification1);
btnNotification2 = (Button)
findViewById(R.id.btnNotification2);
btnNotification3 = (Button)
findViewById(R.id.btnNotification3);
btnNotification4 = (Button)
findViewById(R.id.btnNotification4);
btnNotification5 = (Button)
findViewById(R.id.btnNotification5);
btnNotification6 = (Button)
findViewById(R.id.btnNotification6);
btnNotification7 = (Button)
findViewById(R.id.btnNotification7);
btnNotification8 = (Button)
findViewById(R.id.btnNotification8);
btnNotification9 = (Button)
findViewById(R.id.btnNotification9);
btnNotification1.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.simpleNotification(getApplicationContext());
}
});
btnNotification2.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.openActivityNotification(getApplicationContext());
}
});
btnNotification3.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.twoIconNotification(getApplicationContext());
}
});
btnNotification4.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.bigPictureNotification(getApplicationContext());
}
});
btnNotification5.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.bigTextStyleNotification(getApplicationContext());
}
});
btnNotification6.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.chatStyleNotification(getApplicationContext());
}
});
btnNotification7.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.actionButtonNotification(getApplicationContext());
}
});
btnNotification8.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.customSimpleNotification(getApplicationContext());
}
});
btnNotification9.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v)
{
NotificationGenerator.customBigNotification(getApplicationContext());
}
});
}
}
|
- NotificationGenerator.java
NotificationGenerator.java
import
android.annotation.SuppressLint;
import
android.app.Notification;
import
android.app.NotificationManager;
import
android.app.PendingIntent;
import
android.content.Context;
import
android.content.Intent;
import
android.graphics.BitmapFactory;
import
android.support.v4.app.NotificationCompat;
import
android.widget.RemoteViews;
public class
NotificationGenerator {
public static final String NOTIFY_PREVIOUS = "com.tutorialsface.notificationdemo.previous";
public static final String NOTIFY_DELETE = "com.tutorialsface.notificationdemo.delete";
public static final String NOTIFY_PAUSE = "com.tutorialsface.notificationdemo.pause";
public static final String NOTIFY_PLAY = "com.tutorialsface.notificationdemo.play";
public static final String NOTIFY_NEXT = "com.tutorialsface.notificationdemo.next";
private static final int NOTIFICATION_ID_SIMPLE = 1;
private static final int NOTIFICATION_ID_TWO_ICONS = 2;
private static final int NOTIFICATION_ID_BIG_PIC = 3;
private static final int NOTIFICATION_ID_BIG_TEXT = 4;
private static final int NOTIFICATION_ID_CHAT = 5;
private static final int NOTIFICATION_ID_ACTION_BUTTON = 6;
private static final int NOTIFICATION_ID_CUSTOM_SIMPLE = 7;
private static final int NOTIFICATION_ID_CUSTOM_BIG = 8;
private static final int NOTIFICATION_ID_OPEN_ACTIVITY = 9;
static String songName = "Now
You're Gone";
static String albumName = "Now
Youre Gone - The Album";
public static void
simpleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Simple
Notification");
nc.setContentText("Showing
notification content");
nc.setTicker("Ticker
1");
nc.setNumber(2);
nm.notify(NOTIFICATION_ID_SIMPLE, nc.build());
}
public static void
openActivityNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Creates an
Intent for the Activity
Intent
notifyIntent = new Intent(context, MainActivity.class);
// Sets the
Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// Creates the
PendingIntent
PendingIntent
pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Click to
open Activity");
nc.setContentText("Click
please");
nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY, nc.build());
}
public static void
twoIconNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_call);
nc.setAutoCancel(true);
nc.setContentTitle("Two Icon
Notification");
nc.setContentText("Showing
two icon content");
nc.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
R.drawable.ic_action_person));
nc.setTicker("Ticker
2");
nc.setNumber(3);
nm.notify(NOTIFICATION_ID_TWO_ICONS, nc.build());
}
public static void
bigPictureNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigPictureStyle
bigPictureNotification = new NotificationCompat.BigPictureStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Big
Picture Style");
nc.setContentText("Big
Picture Style Content");
nc.addAction(R.drawable.ic_action_share, "Share", null);
bigPictureNotification.bigPicture(BitmapFactory.decodeResource(context.getResources(),
R.drawable.big_img));
bigPictureNotification.setBigContentTitle("New Big
Picture Title");
nc.setStyle(bigPictureNotification);
nm.notify(NOTIFICATION_ID_BIG_PIC, nc.build());
}
public static void
bigTextStyleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigTextStyle
bigTextNotification = new NotificationCompat.BigTextStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Big Text
Style");
nc.setContentText("Big Text
Style Content");
bigTextNotification.setBigContentTitle("New Big
Text Title");
bigTextNotification.bigText("Starting
with Jelly Bean, Android supports optional actions that are displayed at the
bottom of the notification. With actions, users can handle the most common
tasks for a particular notification from within the notification shade without
having to open the originating application. This speeds up interaction and, in
conjunction with swipe-to-dismiss, helps users to streamline their notification
triaging experience.");
bigTextNotification.setSummaryText("By:
http://developer.android.com/");
nc.setStyle(bigTextNotification);
nm.notify(NOTIFICATION_ID_BIG_TEXT, nc.build());
}
public static void
chatStyleNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.InboxStyle
inboxStyle = new NotificationCompat.InboxStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String[]
lines = new String[7];
lines[0]
= "David@
Hi Howard";
lines[1]
= "Howard@
Hi David";
lines[2]
= "David@
What is the agenda for the upcoming meeting?";
lines[3]
= "Howard@
I will send you a copy";
lines[4]
= "David@
Thank you";
lines[5]
= "Howard@
I will talk to you later";
lines[6]
= "David@
Bye for now";
nc.setSmallIcon(R.drawable.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Chat
Text");
nc.setContentText(lines[0]);
nc.setNumber(7);
for (int i = 0; i <
lines.length; i++) {
inboxStyle.addLine(lines[i]);
}
inboxStyle.setBigContentTitle("New Chat Title");
inboxStyle.setSummaryText("7
messages from 2 conversations.");
nc.setStyle(inboxStyle);
nm.notify(NOTIFICATION_ID_CHAT, nc.build());
}
public static void
actionButtonNotification(Context context) {
NotificationCompat.Builder
nc = new NotificationCompat.Builder(context);
NotificationCompat.BigTextStyle
bigTextNotification = new NotificationCompat.BigTextStyle();
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nc.setSmallIcon(R.drawable.ic_action_end_call);
nc.setAutoCancel(true);
nc.setContentTitle("Big Text
Style Button");
nc.setContentText("(900)022-3365");
nc.addAction(R.drawable.ic_action_call, "Call
back", null);
nc.addAction(R.drawable.ic_action_chat, "Message", null);
bigTextNotification.setBigContentTitle("New Big
Text Title");
bigTextNotification.bigText("Missed
call from (900)022-3365");
nc.setStyle(bigTextNotification);
nm.notify(NOTIFICATION_ID_ACTION_BUTTON, nc.build());
}
public static void
customSimpleNotification(Context context) {
RemoteViews
simpleView = new RemoteViews(context.getPackageName(), R.layout.custom_notification);
Notification
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Custom
Big View").build();
notification.flags |=
Notification.FLAG_AUTO_CANCEL;
notification.contentView = simpleView;
notification.contentView.setTextViewText(R.id.textSongName, songName);
notification.contentView.setTextViewText(R.id.textAlbumName, albumName);
setListeners(simpleView,
context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID_CUSTOM_SIMPLE, notification);
}
@SuppressLint("NewApi")
public static void
customBigNotification(Context context) {
RemoteViews
expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
Notification
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Custom
Big View").build();
notification.flags |=
Notification.FLAG_AUTO_CANCEL;
notification.bigContentView = expandedView;
notification.bigContentView.setTextViewText(R.id.textSongName, songName);
notification.bigContentView.setTextViewText(R.id.textAlbumName, albumName);
setListeners(expandedView,
context);
NotificationManager
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_ID_CUSTOM_BIG, notification);
}
private static void
setListeners(RemoteViews view, Context context) {
Intent
previous = new Intent(NOTIFY_PREVIOUS);
Intent
delete = new Intent(NOTIFY_DELETE);
Intent
pause = new Intent(NOTIFY_PAUSE);
Intent
next = new Intent(NOTIFY_NEXT);
Intent
play = new Intent(NOTIFY_PLAY);
PendingIntent
pPrevious = PendingIntent.getBroadcast(context, 0, previous,
PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent
pDelete = PendingIntent.getBroadcast(context, 0, delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent
pPause = PendingIntent.getBroadcast(context, 0, pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent
pNext = PendingIntent.getBroadcast(context, 0, next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
PendingIntent
pPlay = PendingIntent.getBroadcast(context, 0, play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
}
}
|
- NotificationBroadcast.java
NotificationBroadcast.java
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.widget.Toast;
public class
NotificationBroadcast extends BroadcastReceiver {
@Override
public void
onReceive(Context context, Intent intent) {
if
(intent.getAction().equals(NotificationGenerator.NOTIFY_PLAY)) {
Toast.makeText(context,
"NOTIFY_PLAY", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NotificationGenerator.NOTIFY_PAUSE)) {
Toast.makeText(context,
"NOTIFY_PAUSE", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NotificationGenerator.NOTIFY_NEXT)) {
Toast.makeText(context,
"NOTIFY_NEXT", Toast.LENGTH_LONG).show();
}
else if
(intent.getAction().equals(NotificationGenerator.NOTIFY_DELETE)) {
Toast.makeText(context,
"NOTIFY_DELETE", Toast.LENGTH_LONG).show();
}else if
(intent.getAction().equals(NotificationGenerator.NOTIFY_PREVIOUS)) {
Toast.makeText(context,
"NOTIFY_PREVIOUS", Toast.LENGTH_LONG).show();
}
}
}
|
- AndroidManifest.xml
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorialsface.notificationdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.tutorialsface.notificationdemo.MainActivity"
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:taskAffinity=""
>
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<receiver android:name=".NotificationBroadcast" >
<intent-filter>
<action android:name="com.tutorialsface.notificationdemo.previous"/>
<action android:name="com.tutorialsface.notificationdemo.delete"
/>
<action android:name="com.tutorialsface.notificationdemo.pause"
/>
<action android:name="com.tutorialsface.notificationdemo.next"
/>
<action android:name="com.tutorialsface.notificationdemo.play"
/>
</intent-filter>
</receiver>
</application>
</manifest>
|
No comments:
Post a Comment