Friday, 27 March 2015

Implementing Different Notifications in Android

Hey Guys,

I'm just providing a demo code for the newest notification going now a days in our android phones.

Try these methods and Enjoy new phase of notifications in development.


1. Set Normal Notification 


private Notification setNormalNotification() {
Bitmap mNotificationPicture = null;

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Setup an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// TaskStackBuilder ensures that the back button follows the recommended
// convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Normal Notification")
.setContentText("This is an example of a Normal Style.")
.build();
}

Result :



2. Big Text Style Notification


private Notification setBigTextStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with BigTextStyle subclass.
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle("Big Text Expanded");
notiStyle.setSummaryText("Nice big text.");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

CharSequence bigText = "This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.This is a demo Big Text Style notification.";
notiStyle.bigText(bigText);

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Big Text Normal")
.setContentText("This is an example of a Big Text Style.")
.setStyle(notiStyle).build();
}

Result :



3. Big Picture Style Notification

private Notification setBigPictureStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Add the big picture to the style.
notiStyle.bigPicture(mNotificationPicture);

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Big Picture Normal")
.setContentText("This is an example of a Big Picture Style.")
.setStyle(notiStyle).build();
}

Result :



4. Inbox Style Notification

private Notification setInboxStyleNotification() {
Bitmap mNotificationPicture = null;

// Create the style object with InboxStyle subclass.
NotificationCompat.InboxStyle notiStyle = new NotificationCompat.InboxStyle();
notiStyle.setBigContentTitle("Inbox Style Expanded");

// Add the multiple lines to the style.
// This is strictly for providing an example of multiple lines.
for (int i = 1; i <= 5; i++) {
notiStyle.addLine("(" + i + " of 6) Line one here.");
}
notiStyle.setSummaryText("+6 more Line Samples");

try {
mNotificationPicture = BitmapFactory
.decodeStream((InputStream) new URL(sample_url)
.getContent());
} catch (IOException e) {
e.printStackTrace();
}

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself).
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

return new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setLargeIcon(mNotificationPicture)
.setContentIntent(resultPendingIntent)
.addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
.addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
.setContentTitle("Inbox Style Normal")
.setContentText("This is an example of a Inbox Style.")
.setStyle(notiStyle).build();
}


Result :



5. Custom View Notification

private Notification setCustomViewNotification() {

// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(this, ResultActivity.class);

// This ensures that the back button follows the recommended convention
// for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);

// Adds the Intent that starts the Activity to the top of the stack.
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);

// Create remote view and set bigContentView.
RemoteViews expandedView = new RemoteViews(this.getPackageName(),
R.layout.notification_custom_remote);
expandedView.setTextViewText(R.id.text_view, "Neat logo!");

Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle("Custom View").build();

notification.bigContentView = expandedView;

return notification;
}

Result :






Happy Coding !! Cheers !!





No comments:

Post a Comment