Android custom notification is not that simple

Source: Internet
Author: User

Background

Recently, you need to implement a custom notification feature. Find code on the net, the solution is through remoteviews to achieve. But in the implementation process encountered a lot of problems, there is no good article on the Internet to describe these problems, so here to make a summary, I hope we can go a little detour.

Implement Remoteviews Custom View

This is the most basic point of knowledge, although the custom notification should be clear, but I think it is necessary to take a bit. It is primarily used in appwidget and notification, which describes a view that is displayed in other processes. The following is an example code. From here we can see that Remoteviews provides some ways to change the value of its child view, such as setting TextView text.

RemoteviewsRemoteviews=NewRemoteviews(Context.Getpackagename(),R.Layout.View_notification_type_0);Remoteviews.Settextviewtext(R.Id.Title_tv,Title);Remoteviews.Settextviewtext(R.Id.Content_tv,Content);Remoteviews.Settextviewtext(R.Id.Time_tv,GetTime());Remoteviews.Setimageviewresource(R.Id.Icon_iv,R.Drawable.Logo);Remoteviews.Setint(R.Id.Close_iv,"Setcolorfilter",Geticoncolor());IntentIntent=NewIntent(Context,Mainactivity.Class);Intent.PutExtra(Notice_id_key,Notice_id_type_0);Intent.SetFlags(Intent.Flag_activity_new_task|Intent.Flag_activity_clear_task);IntRequestcode=(Int)Systemclock.Uptimemillis();PendingintentPendingintent=Pendingintent.Getactivity(Context,Requestcode,Intent,Pendingintent.Flag_update_current);Remoteviews.Setonclickpendingintent(R.Id.Notice_view_type_0,Pendingintent);IntRequestCode1=(Int)Systemclock.Uptimemillis();IntentIntent1=NewIntent(Action_close_notice);Intent1.PutExtra(notice_id_keynotice_id_type_0); pendingintent pendingintent1 = PendingIntentgetbroadcast (context Requestcode1intent1pendingintent flag_update_currentremoteviews. (r. Id.pendingintent1       

Here are a few things to keep in mind.

Setint

This method is used to invoke a method that requires an int parameter in the sub-view. As the following code, the Setcolorfilter method with ID Close_iv is called and the return value of the parameter is Geticoncolor ().

remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());
Set the click Pendingintent for different areas

The default notification can only set the overall click event through Setcontentintent. But through remoteviews we can set different click events in different places, of course, the events here refer to pendingintent. As below, set the click R.id.notice_view_type_0 to open an activity, while clicking on R.id.close_iv will issue a broadcast that can be done by this broadcast radio receiver, such as this is to close the current notification. Alternatively, you can open a service.

Pendingintent.Getactivity(Context,Requestcode,Intent,Pendingintent.Flag_update_current);Remoteviews.Setonclickpendingintent(R.Id.Notice_view_type_0,Pendingintent);IntRequestCode1=(Int)Systemclock.Uptimemillis();IntentIntent1=NewIntent(Action_close_notice);Intent1.PutExtra(notice_id_keynotice_id_type_0); pendingintent pendingintent1 = PendingIntentgetbroadcast (context Requestcode1intent1pendingintent flag_update_currentremoteviews. (r. Id.pendingintent1       
Set Custom view for notifications

Above we got the custom remoteviews. The notification of the custom view can be generated with this code, note that the SetContent () method is used here. This is the method that the online custom notification will use.

Notification notification = new NotificationCompat.Builder(context).setContent(remoteViews).build();

But it will have a problem.

The notification obtained by means of the SetContent () method are fixed high. If the view height is larger than the default height, a portion of it will not appear. Such as

By default the notification height is 64DP, and of course ROM may differ somewhat. Normal text can be displayed if it is less than two lines in the case.

So how to do wrap_content. Need to use some black tech. As follows:

Notificationcompat.BuilderBuilder=NewNotificationcompat.Builder(Context);if (android. Os...>= 16) { span class= "n" >notification = builder. Build (); notification.= remoteviews;} notification.= remoteviews       

In order to understand the above code, we need to identify a problem that we can easily ignore, that is, notifications are expandable and closed. Take a look at the following two photos. Also is the notice of NetEase cloud music, figure one is bigger than figure two. In fact, the show is the NetEase cloud music notification of the unfolding state, using two fingers on the slide can be shrunk, that is, figure two.

In the above code we set the Bigcontentview, which is the expanded custom view, and the Contentview is the view when it is closed.

Note that Bigcontentview is introduced in sdk16, so it needs to be judged. If it is less than sdk16, it will only be high.

Note that the maximum height of the Bigcontentview is 256DP

Note that the settings of Bigcontentview and Contentview cannot be reversed, so the Contentview does not show up in the pro-test.

It is also important to note that some ROMs may not support the unwinding of a notification, only the expanded view is displayed after Bigcontentview has been set, and only the view is shown by default. Other ROMs, such as Meizu's FlyMe, are not tested if the reader knows to share them.

Background color Matching

The notification background color for different ROMs is different, so be aware of the UI. There are two main types of cases.

    • The background color is a transparent black, such as MIUI, FlyMe.
    • The background color is white, such as ROM after native 5.0, part ROM of Huawei.

There are two main types of scenarios.

Fixed background color

That is, set a fixed background color, both text and icon color can be fixed. Such as. This has a drawback, we also see in the figure, that is, some ROM notification will have a left or right padding, such as MIUI is particularly obvious, if the fixed background color will be very difficult to see. So although this method is simple, it is not recommended.

Transparent background color

Another way is to make the background transparent. What about the color of the text and icon? It's easy to follow the style of the text in the system's notification. The style of TextView is set as follows in the default notification of info. Other related style includes TextAppearance.StatusBar.EventContent.Line2, TextAppearance.StatusBar.EventContent.Info and so on.

        <TextView            android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Info" tools:text="41个同校小伙伴参与讨论" android:layout_marginTop="4dp" android:singleLine="true"/>

One thing to note is that after Android5.0, a different style name is used to indicate the notification style. We need to create a layout-v21 folder and a new custom notification style that is used after 5.0. The following is also the style of setting TextView for Info, but we are using a style of @android:style/textappearance.material.notification.info.

<TextViewandroid:id= "@+id /content_tv " android:layout_width=" wrap_content " android:layout_height= "wrap_content"  android:textsize= "9SP"  android:textappearance= "@android: Style/textappearance.material.notification.info" Span class= "Hljs-rule" > tools:text= "41 partners in the same school participate in the discussion"  android:layout_margintop= "4DP"  android: Singleline= "true" />         

In addition, if the custom view has an icon, then the icon color also needs to adapt to the background, you can choose a gray, such as #999999, native Android black and white text content color is the value.

or set different colors according to the different background color, through the Setint method mentioned above. ImageView's Setcolorfilter method lets you set the pattern color to a solid color. But for the moment I haven't found a good way to get the background color of the default notification, if the reader finds a hope to tell.

remoteViews.setInt(R.id.close_iv, "setColorFilter", getIconColor());
Final effect

Summarize

These are some of the problems and solutions that I encountered in my custom notification. At present, there are two points to be further supplemented and perfected.

    • Gets the default notification background color, or a scheme that makes the icon color match the background color.
    • does not support notification expansion of the closed ROM, currently known only FlyMe.
Sample Code Address

Https://github.com/beautifulSoup/CNotification

Android custom notification is not that simple

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.