Create custom notification, android
Asked Answered
V

2

21

I want to create a notification that like the red bordered notification in this image:

enter image description here

I know how to create normal notifications like the blue bordered notifications in this image, but I want to show an icon and about 3 lines of information near that. How can I do that? Any suggestion will be appreciated.

Vestibule answered 23/4, 2013 at 11:46 Comment(0)
P
48

Add RemoteViews in notification. Here is a sample:

enter image description here

var remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
var mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContent(remoteViews);

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, test.class);

// The stack builder object will contain an artificial back stack for
// the started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(test.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.button1, resultPendingIntent);

var notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
notificationManager.notify(100, mBuilder.build()); 

widget.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="DJ notification"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close Me" />
</LinearLayout> 

check this article there is more style avaialbe

Android Developer

Edited:

The NotificationCompat.Builder is the most easy way to create Notifications on all Android versions. You can even use features that are available with Android 4.1. If your app runs on devices with Android >=4.1 the new features will be used, if run on Android <4.1 the notification will be an simple old notification.

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

Paleolithic answered 23/4, 2013 at 11:54 Comment(8)
Thanks, good code, but as you mentioned this is for API>11. but I want my app to run on API>7.Vestibule
Is it possible to handle click or use custom View ? something like class MyBTN extends RelativeLayout{...}Tonnie
@DhawalSodhaParmar: how to remove extra padding at bottom area which is visible also in your screenshotCitystate
@DhawalSodhaParmar: Hi, Is there any solution for padding issue?Citystate
@Erick: Sagar, I've already done it.it's not working in some devices.Citystate
@MehulJoisar ohhk.. i didnt implement that but i think its default size may be.Ensemble
@Erick: only one thing worked in my case across all the versions and devices to use such custom view and show it in center.I ended up setting 64dp height of parent and center_vertical gravity to its children. reference: documentationCitystate
Thanx a lot Dhawal your blog helpedJostle
S
8

Expanded notifications are available from Android 4.1 onwards to handle these scenarios. If you are using Notification.Builder or NotificationCompat.Builder, you would set up a normal Builder and a separate Builder for the expanded notification, using NotificationCompat.InboxStyle or one of the other styles, and connect the two.

Sorehead answered 23/4, 2013 at 11:50 Comment(2)
Thanks, any link or tutorial available ?Vestibule
@Reza_Rg: github.com/commonsguy/cw-omnibus/tree/master/Notifications/…Sorehead

© 2022 - 2024 — McMap. All rights reserved.