http://www.framentos.com/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/
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
<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
<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
<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:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
and create a new Notification:
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 contentViewfield of our 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:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
Set some flags for notification behaviour and launch 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:
'모바일개발(Mobile Dev) > 안드로이드개발(Android)' 카테고리의 다른 글
userful information (0) | 2017.09.10 |
---|---|
android icon guide (0) | 2017.09.10 |
memory leak in asynctask (0) | 2017.09.10 |
제품(Products) 탭 (0) | 2017.09.02 |
oAuth의 인증 플로우 (0) | 2017.09.02 |