http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/


In this article, we will explain how is possible to create a custom notification layout to replace the standard one in the notification bar.
To do it we need to use the Android object RemoteViews, in the library android.widget.RemoteViews. This is available since API level 1 so it will work for every kind of Android versions.
Let’s create our custom notification!


First of all, we just create a custom layout in an xml file. Let’s call it custom_notification.xml

<?xml version = "1.0" encoding = "utf-8" ?>
<RelativeLayout xmlns :android = "http://schemas.android.com/apk/res/android"
android :id = "@+id/custom_notification"
android :layout_width = "fill_parent"
android :layout_height = "fill_parent"
android :gravity = "center" >

<ImageView
android :id = "@+id/notifiation_image"
android :layout_width = "wrap_content"
android :layout_height = "wrap_content"
/>

<TextView
android :id = "@+id/notification_title"
android :layout_width = "wrap_content"
android :layout_height = "wrap_content"
android :layout_toRightOf = "@id/notification_image"
android :layout_alignTop = "@+id/notification_image"
style = "@style/NotificationTitle"
/>

<TextView
android :id = "@+id/notification_text"
android :layout_width = "wrap_content"
android :layout_height = "wrap_content"
android :layout_toRightOf = "@+id/notification_image"
android :layout_below = "@+id/notification_title"
style = "@style/NotificationText"
/>

</RelativeLayout >

This layout will be the one showed into the notification bar.

IMPORTANT: All the TextView of your custom notification must have a style declared to prevent issues caused by the different background colors of the notification bar for various version of Android.

Our app will read the style from different files depending on the Android version. If the version is 2.2 or lower, the style must be defined in the file res/values/styles.xml

<?xml version = "1.0" encoding = "utf-8" ?>
<resources >
<style name = "NotificationText" >
<item name = "android:textColor" >?android :attr /textColorPrimaryInverse </item >
</style >
<style name = "NotificationTitle" >
<item name = "android:textColor" >?android :attr /textColorPrimaryInverse </item >
<item name = "android:textStyle" >bold </item >
</style >
</resources >

For Android 2.3 and higher versions, we need to define the style in res/values-v9/styles.xml

<?xml version = "1.0" encoding = "utf-8" ?>
<resources >
<style name = "NotificationText" parent = "android:TextAppearance.StatusBar.EventContent" />
<style name = "NotificationTitle" parent = "android:TextAppearance.StatusBar.EventContent.Title" />
</resources >

IMPORTANT: There is an error in the attributes provided by Android, the procedure on Android DEV Guide, will lead you to have errors in colors. In order to fix it, we modified the attribute textColorPrimary in textColorPrimaryInverse

From our Java code, we have to get the NotificationManager:

int NOTIFICATION_ID = 1 ;
String ns = Context. NOTIFICATION_SERVICE ;
NotificationManager mNotificationManager = (NotificationManager ) getSystemService (ns ) ;

and create a new Notification:

int icon = R. drawable. icon ;
long when = System. currentTimeMillis ( ) ;
Notification notification = new Notification (icon, getString (R. string. text ), when ) ;

Than we have to instantiate a RemoteViews object that inflates our custom layout file, fill the View objects and pass the RemoteViews to the contentView field of our Notification.

RemoteViews contentView = new RemoteViews (getPackageName ( ), R. layout. custom_notification ) ;
contentView. setImageViewResource (R. id. notification_image, R. drawable. notification_image ) ;
contentView. setTextViewText (R. id. notification_title, "My custom notification title" ) ;
contentView. setTextViewText (R. id. notification_text, "My custom notification text" ) ;
notification. contentView = contentView ;

We instantiate the intent to call our Activity when the notification is clicked and add it to the contentIntent field of the Notification:

Intent notificationIntent = new Intent ( this, MyActivity. class ) ;
PendingIntent contentIntent = PendingIntent. getActivity ( this, 0, notificationIntent, 0 ) ;

notification. contentIntent = contentIntent ;

Set some flags for notification behaviour and launch the notification:

notification. flags |= Notification. FLAG_NO_CLEAR ; //Do not clear the notification
notification. defaults |= Notification. DEFAULT_LIGHTS ; // LED
notification. defaults |= Notification. DEFAULT_VIBRATE ; //Vibration
notification. defaults |= Notification. DEFAULT_SOUND ; // Sound

mNotificationManager. notify (NOTIFICATION_ID, notification ) ;

To cancel the notification we just need to do:

mNotificationManager. cancel (NOTIFICATION_ID ) ;

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)Studio 上的 HttpClient 插
  2. android删除第三方jar的方法
  3. Android-->Android(安卓)原生支持圆角图
  4. View 的 android:visibility属性的讨论
  5. Eclipse+CDT+GDB调试Android(安卓)NDK程
  6. Android(安卓)如何将Canvas上绘制的内容
  7. 第二个Activity和Android(安卓)ListView
  8. android Notification (2)
  9. 2018-06-15 Android加载GIF图片的两种方
  10. Android传感器分析 seneor