How to create Custom Notification in android

2019-03-21 01:59发布

问题:

I need to create a custom notification instead of the default notification in android. Current notification have a icon,heading and message like below image

I want it customise like this

how can i achieve this

回答1:

Notification views

Normal View - A notification in normal view appears in an area that’s up to 64 dp tall. Even if you create a notification with a big view style, it will appear in normal view until it’s expanded.

Content title
Large icon
Content text
Content info
Small icon
Notification time

Normal View Like

Big View - A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications were first introduced in Android 4.1 JellyBean [API 16]. Expandable notifications were designed to support rich notification style objects called Notification.Style.

Big View Like

Go to this link expandable-notifications-android

More information on official docs



回答2:

I have create notification usingby following http://developer.android.com/wear/notifications/creating.html

Add code for create notification.

// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_event)
        .setLargeIcon(BitmapFractory.decodeResource(
                getResources(), R.drawable.notif_background))
        .setContentTitle(eventTitle)
        .setContentText(eventLocation)
        .setContentIntent(viewPendingIntent)
        .addAction(R.drawable.ic_map,
                getString(R.string.map), mapPendingIntent)
        .setStyle(bigStyle);


回答3:

That's called Expanded layouts which is available right from Jelly Bean version.

There are 2 views of Notifications:

  1. Normal View
  2. Big View

A notification’s big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications were first introduced in Android 4.1 JellyBean [API 16].

In your case, you just want to display big picture in notification, give Notification.BigPictureStyle a try:

Bitmap remote_picture = null;

// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new 
        NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Big Picture Expanded");
notiStyle.setSummaryText("Nice big picture.");

try {
        remote_picture = BitmapFactory.decodeStream(
                (InputStream) new URL(sample_url).getContent());
} catch (IOException e) {
        e.printStackTrace();
}

// Add the big picture to the style.
notiStyle.bigPicture(remote_picture);


回答4:

you can use NotificationCompat that need remoteview . below i set my custom layout named
activity_downlaodactivity

NotificationManager manager =   (NotificationManager)       getSystemService(Context.NOTIFICATION_SERVICE);
    RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.activity_downlaodactivity);


   android.support.v4.app.NotificationCompat.Builder    mBuilder =
            new android.support.v4.app.NotificationCompat.Builder(this)
            .setContent(contentView)

   manager.notify(0, mBuilder.build());