Android system (38) --- Android Notifaction from 4.0 to 7.0 Android Notifaction from 4.0 to 7.0

Android Notifaction from 4.0 to 7.0

Figure 1 4.0 Notification style

4.4.4

Figure 2 5.0 6.0 Notification Style

5.0 6.0

Figure 3 7.0 Notification style

7

compatible

So many versions, how to be compatible, it doesn't matter, leave it to 
android.support.v7.app.NotificationCompat

Basic usage

    NotificationManager nm = (NotificationManager) ctx.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
  • 1
  • 2
  • 3

Set the property of notifaction through builder

For specific methods, see: Official API, available in China

Advanced usage

it's these styles

NotificationCompat.BigPictureStyle

NotificationCompat.BigTextStyle

NotificationCompat.InboxStyle

NotificationCompat.MediaStyle

NotificationCompat.DecoratedCustomViewStyle

NotificationCompat.MessagingStyle

Tips

banner

two ways

1.builder.setPriority(Notification.PRIORITY_MAX);

2.builder.setFullScreenIntent (intent, false);

Notification no sound, no vibration, no LED breathing light flashing

        long[] pattern = {0,0};
        builder.setVibrate(pattern);
        builder.setLights(Color.rgb(0,0,0),0,0);
        builder.setSound(null);
  • 1
  • 2
  • 3
  • 4
  • 5

Asynchronous download of the image displayed in the notification


If you use the glide image to load the frame, then congratulations on your winning. Today I will introduce glide. The same Picasso also has a similar method. The usage is basically the same.  That is to download first and then display the notification, here we also talk about two

1. Directly display, asynchronously load the notification area image

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView = new RemoteViews(ctx.getPackageName(), R.layout.notification_expanded);
        expandedView.addView(R.id.content_rl, notification.contentView.clone());
        notification.bigContentView = expandedView;
        NotificationTarget notificationTarget = new NotificationTarget(context.getApplicationContext(), expandedView, R.id.expanded_imageView, notification, id);
        Glide.with(context.getApplicationContext()) // safer!
                .load(bigPicture)
                .asBitmap()
                .into(notificationTarget);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

layout file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right|top"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/content_rl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></RelativeLayout>


<ImageView
    android:id="@+id/expanded_imageView"
    android:layout_width="match_parent"
    android:layout_height="152dp"
    android:contentDescription="@string/app_name"
    android:scaleType="centerCrop" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2. First download and then display 
rewrite NotificationTarget, and then get the picture and then display the notification

There are always pits waiting for you to jump

1. After setting the banner, unlock the PendingIntent event that directly triggers the notification after the screen is locked

builder.setFullScreenIntent(intent,false);
  • 1
  • 2

source code

/**
     * An intent to launch instead of posting the notification to the status bar.
     * Only for use with extremely high-priority notifications demanding the user's
     * <strong>immediate</strong> attention, such as an incoming phone call or
     * alarm clock that the user has explicitly set to a particular time.
     * If this facility is used for something else, please give the user an option
     * to turn it off and use a normal notification, as this can be extremely
     * disruptive.
     *
     * <p>
     * The system UI may choose to display a heads-up notification, instead of
     * launching this intent, while the user is using the device.
     * </p>
     *
     * @param intent The pending intent to launch.
     * @param highPriority Passing true will cause this notification to be sent
     *          even if other notifications are suppressed.
     *
     * @see Notification#fullScreenIntent
     */
    public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
        mFullScreenIntent = intent;
        setFlag(FLAG_HIGH_PRIORITY, highPriority);
        return this;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

The general meaning is: after setting setFullScreenIntent, it means that the priority of this notification is as high as the priority of incoming calls, and it will be displayed to you directly. It is recommended to set the switch so that users can freely choose. 
Therefore, it is recommended to use setting notification priority to display the banner.

2.setContentInfo与setContentText

At first glance, it seems that the difference is not big, but the actual difference is as follows:

In 7.0 and above, setContentInfo represents the red frame area, and setContentText represents the black frame area

/**
     * Set the large text at the right-hand side of the notification.
     */
    public Builder setContentInfo(CharSequence info) {
        mContentInfo = limitCharSequenceLength(info);
        return this;
    }





/**
     * Set the title (first row) of the notification, in a standard notification.
     */
    public Builder setContentTitle(CharSequence title) {
        mContentTitle = limitCharSequenceLength(title);
        return this;
    }

    /**
     * Set the text (second row) of the notification, in a standard notification.
     */
    public Builder setContentText(CharSequence text) {
        mContentText = limitCharSequenceLength(text);
        return this;
    }

    /**
     * Set the third line of text in the platform notification template.
     * Don't use if you're also using {@link #setProgress(int, int, boolean)};
     * they occupy the same location in the standard template.
     * <br>
     * If the platform does not provide large-format notifications, this method has no effect.
     * The third line of text only appears in expanded view.
     * <br>
     */
    public Builder setSubText(CharSequence text) {
        mSubText = limitCharSequenceLength(text);
        return this;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324621922&siteId=291194637