How to create a custom notification on Android

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值