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);
}
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