Notification creation in android has updated with few methods are deprecated in Marsh Mallows Version
I have created notification in differert formats
check below Fragment class and xml files
/**
* A fragment that enables display of notifications.
*/
public class ActiveNotificationFragment extends Fragment {
private static final String TAG = "ActiveNotificationFragment";
private NotificationManager mNotificationManager;
private TextView mNumberOfNotifications;
// Every notification needs a unique ID otherwise the previous one would be overwritten.
private int mNotificationId = 0;
private PendingIntent mDeletePendingIntent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_notification_builder, container, false);
}
@Override
public void onResume() {
super.onResume();
updateNumberOfNotifications();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mNotificationManager = (NotificationManager) getActivity().getSystemService(
Context.NOTIFICATION_SERVICE);
mNumberOfNotifications = (TextView) view.findViewById(R.id.number_of_notifications);
// Supply actions to the button that is displayed on screen.
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_notification:
addNotificationAndReadNumber();
break;
case R.id.add_notification_big:
addBigText();
break;
case R.id.add_notification_big_image:
addBigImage();
break;
case R.id.add_notification_heads:
addHeadsUpNotification();
break;
case R.id.add_notification_action:
customNotification();
break;
}
}
};
view.findViewById(R.id.add_notification).setOnClickListener(onClickListener);
view.findViewById(R.id.add_notification_big).setOnClickListener(onClickListener);
view.findViewById(R.id.add_notification_big_image).setOnClickListener(onClickListener);
view.findViewById(R.id.add_notification_heads).setOnClickListener(onClickListener);
view.findViewById(R.id.add_notification_action).setOnClickListener(onClickListener);
// Create a PendingIntent to be fired upon deletion of a Notification.
Intent deleteIntent = new Intent("YOUR ACTIVITY/BORADCASTRECEIVER";
mDeletePendingIntent = PendingIntent.getBroadcast(getActivity(),
2 /* requestCode */, deleteIntent, 0);
}
/**
* Add a new {@link Notification} with sample data and send it to the system.
* Then read the current number of displayed notifications for this application.
*/
private void addNotificationAndReadNumber() {
// [BEGIN create_notification]
// Create a Notification and notify the system.
final Notification.Builder builder = new Notification.Builder(getActivity())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.sample_notification_content))
.setAutoCancel(true)
.setDeleteIntent(mDeletePendingIntent);
builder.setContentIntent(getPendingIntent());
final Notification notification = builder.build();
mNotificationManager.notify(++mNotificationId, notification);
// [END create_notification]
Log.i(TAG, "Add a notification");
updateNumberOfNotifications();
}
private void addBigText()
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Event tracker")
.setContentText("Events received")/*.
setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVibrate(new long[] {1, 1, 1})*/;
/* Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(getActivity(), MainActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0,
push, PendingIntent.FLAG_CANCEL_CURRENT);
mBuilder
.setContentText("A Heads-Up notification for Lollipop and above")
.setFullScreenIntent(fullScreenPendingIntent, true);
*/
// notificationManager.notify(HEADS_UP_NOTIFICATION_ID, notificationBuilder.build());
mBuilder .setAutoCancel(true);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]+" : "+i);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(getPendingIntent());
Notification notification=mBuilder.build();
// mBuilder.setAutoCancel(true);
mNotificationManager.notify(++mNotificationId, notification);
updateNumberOfNotifications();
}
private void addBigImage()
{
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle("Event tracker")
.setContentText("Events received")
;
NotificationCompat.BigPictureStyle inboxStyle =
new NotificationCompat.BigPictureStyle();
String[] events = new String[6];
inboxStyle.setBigContentTitle("Event tracker details:");
inboxStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
// Moves events into the expanded layout
mBuilder.setAutoCancel(true);
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(getPendingIntent());
Notification notification=mBuilder.build();
mNotificationManager.notify(++mNotificationId, notification);
updateNumberOfNotifications();
}
private void addHeadsUpNotification()
{
Notification.Builder builder=new Notification.Builder(getActivity());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setPriority(Notification.PRIORITY_HIGH);
builder.setVibrate(new long[]{100,200,100});
builder.setContentTitle("HeadsUpNotification");
builder.setContentIntent(getPendingIntent());
builder.setAutoCancel(true);
mNotificationManager.notify(++mNotificationId,builder.build());
}
/**
* Request the current number of notifications from the {@link NotificationManager} and
* display them to the user.
*/
protected void updateNumberOfNotifications() {
// [BEGIN get_active_notifications]
// Query the currently displayed notifications.
if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M)
return;
final StatusBarNotification[] activeNotifications = mNotificationManager
.getActiveNotifications();
// [END get_active_notifications]
final int numberOfNotifications = activeNotifications.length;
mNumberOfNotifications.setText(getString(R.string.active_notifications,
numberOfNotifications));
Log.i(TAG, getString(R.string.active_notifications, numberOfNotifications));
}
private PendingIntent getPendingIntent()
{
Intent intent=new Intent(getActivity(),SeconActivity.class);
PendingIntent pending=PendingIntent.getActivity(getActivity(),0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
return pending;
}
private void customNotification()
{
RemoteViews remoteview=new RemoteViews(getActivity().getPackageName(),R.layout.remoteview);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getActivity())
.setContent(remoteview).setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
Notification notification=notificationBuilder.build();
remoteview.setOnClickPendingIntent(R.id.img,PendingIntent.getActivity(getActivity(),0,new Intent(getActivity(),SeconActivity.class),PendingIntent.FLAG_CANCEL_CURRENT));
notification.bigContentView = remoteview;
notification.flags|=Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(++mNotificationId,notification);
}
}
XML File
------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/add_notification"
android:text="@string/add_a_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add_notification_big"
android:text="BigText Notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add_notification_big_image"
android:text="BigImage Notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add_notification_heads"
android:text="HeadsUp notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/add_notification_action"
android:text="Action notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
style="@android:style/TextAppearance.Material.Large"
android:id="@+id/number_of_notifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
remoteview layout
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/img"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#d94cd9"
android:text="Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#e10a23"
android:text="Description"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#c4612c"
android:text="Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#a7ab42"
android:text="Description"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#701770"
android:text="Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#5a51bc"
android:text="Description"/>
</LinearLayout>
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_alignParentRight="true"/>
</RelativeLayout>