Friday, February 20, 2015

Multiline Notification with user define tone in android

Today, I am explaining how to generate an individual notification with multiple lines of text similar to gmail.

Notification : A message which display to the user outside of the application's. User check the notification and open the notification for viewing the complete message.


private static void generateNotification(Context context, String message) {

int icon = R.drawable.logo_small;

NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String title = "Title";

Intent notificationIntent = new Intent(context, YourActivity.class);

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);

//set sound for notifications

Uri defaultSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

if (defaultSound == null) {
defaultSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
if (defaultSound == null) {
defaultSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
}
}

Builder builder = new Notification.Builder(context)
.setContentTitle(title).setContentText(message)
.setContentIntent(intent).setSmallIcon(icon)
.setLights(Color.YELLOW, 1, 2).setAutoCancel(true)
.setSound(defaultSound);

//multiline notification

Notification not = new Notification.BigTextStyle(builder).bigText(
message).build();


not.defaults |= Notification.DEFAULT_VIBRATE;
not.defaults |= Notification.DEFAULT_SOUND;

notificationManager.notify(0, not);

}

0 comments:

Post a Comment

Don't lose faith when you see others receive answers to their prayers

An elephant and a dog became pregnant at same time. Three months down the line the dog gave birth to six puppies. Six months later the dog...

 

G-Expo Template by Ipietoon Cute Blog Design