{"id":15247,"date":"2014-08-21T15:35:52","date_gmt":"2014-08-21T10:05:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=15247"},"modified":"2016-08-30T11:00:24","modified_gmt":"2016-08-30T05:30:24","slug":"android-notifications-with-images-and-text-together","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/android-notifications-with-images-and-text-together\/","title":{"rendered":"Android Notifications with Images and Text Together"},"content":{"rendered":"<p>We all love notifications of <a title=\"android app development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">android applications<\/a> and its notification bar. But it would be much better if we can show the user the rich notifications instead of those boring text notifications. But it is not simple task that it sounds to be.<\/p>\n<p>The problem is, when we try to display image with text then what we get only image or text depending on devices but not simultaneouly. If we get the text then to reveal the image we will have to expand the image by gesture and vice-versa otherwise.<\/p>\n<p>Here, I will show you how to achieve this functionality. I discovered two\u00a0solution for this problem.<\/p>\n<p>Here is my fist solution that utilize Styles:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nNotificationManager notificationManager = (NotificationManager)<br \/>\n          context.getSystemService(Context.NOTIFICATION_SERVICE);<\/p>\n<p>Intent notificationIntent = new Intent(context, MainActivity.class);<\/p>\n<p>notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP<br \/>\n | Intent.FLAG_ACTIVITY_SINGLE_TOP);<\/p>\n<p>PendingIntent intent =<br \/>\n        PendingIntent.getActivity(context, 0,<br \/>\n         notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);<\/p>\n<p>Bitmap bitmap = BitmapFactory.decodeResource(getResources(),<br \/>\n                                           R.drawable.large_icon);<br \/>\nString message=&quot;Hello Notification with image&quot;;<br \/>\nString title=&quot;Notification !!!&quot;;<br \/>\nint icon = R.drawable.ic_launcher;<br \/>\nlong when = System.currentTimeMillis();<\/p>\n<p>notification = new NotificationCompat.Builder(context)<br \/>\n .setContentTitle(title)<br \/>\n .setContentText(message)<br \/>\n .setContentIntent(intent)<br \/>\n .setSmallIcon(icon)<br \/>\n .setWhen(when)<br \/>\n .setStyle(new NotificationCompat.BigPictureStyle()<br \/>\n .bigPicture(bitmap).setSummaryText(message))<br \/>\n .build();<\/p>\n<p>notification.flags |= Notification.FLAG_AUTO_CANCEL;<\/p>\n<p>\/\/ Play default notification sound<br \/>\n notification.defaults |= Notification.DEFAULT_SOUND;<br \/>\n notification.defaults |= Notification.DEFAULT_VIBRATE;<br \/>\n notificationManager.notify(0, notification);<br \/>\n[\/sourcecode]<\/p>\n<p>In above code the I am calling Builder.setStyle() method passing new instance of NotificationCompat.BigPictureStyle() and providing it with a bitmap and message by calling its respective methods.<\/p>\n<p>And here is another solution that utilzes Dynamic Layout:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\n String title=&quot;Notification !!!&quot;;<br \/>\n int icon = R.drawable.ic_launcher;<br \/>\n long when = System.currentTimeMillis();<\/p>\n<p>Notification notification = new Notification(icon, title, when);<\/p>\n<p> NotificationManager mNotificationManager =<br \/>\n      (NotificationManager)getSystemService(NOTIFICATION_SERVICE);<\/p>\n<p> RemoteViews contentView = new RemoteViews<br \/>\n                 (getPackageName(), R.layout.custom_notification);<br \/>\n contentView.setImageViewResource(R.id.image,<br \/>\n                                  R.drawable.ic_launcher);<br \/>\n contentView.setTextViewText(R.id.title, title);<br \/>\n contentView.setTextViewText(R.id.text,<br \/>\n                               &quot;This is a custom layout&quot;);<br \/>\n notification.contentView = contentView;<\/p>\n<p> Intent notificationIntent = new Intent(this, MainActivity.class);<br \/>\n PendingIntent contentIntent = PendingIntent.getActivity(this,0,<br \/>\n                                           notificationIntent, 0);<br \/>\n notification.contentIntent = contentIntent;<\/p>\n<p> notification.flags |=<br \/>\n      Notification.FLAG_NO_CLEAR; \/\/Do not clear  the notification<br \/>\n notification.defaults |= Notification.DEFAULT_LIGHTS; \/\/ LED<br \/>\n notification.defaults |= Notification.DEFAULT_VIBRATE;\/\/Vibration<br \/>\n notification.defaults |= Notification.DEFAULT_SOUND; \/\/ Sound<\/p>\n<p> mNotificationManager.notify(1, notification);<br \/>\n[\/sourcecode]<\/p>\n<p>In this code I am using the RemoteViews class to set the image and text together. For getting this class to work we need to pass two argument, first is application package and other is layout file that describes an ImageView and two TextView.<\/p>\n<p>And that is all we need to implement this feature correctly.<\/p>\n<p>Have a nice time with your rich notifications. \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all love notifications of android applications and its notification bar. But it would be much better if we can show the user the rich notifications instead of those boring text notifications. But it is not simple task that it sounds to be. The problem is, when we try to display image with text then [&hellip;]<\/p>\n","protected":false},"author":131,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":15},"categories":[518],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15247"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=15247"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/15247\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=15247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=15247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=15247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}