Android Notification analysis – A variety of issues you may encounter

Source: Internet
Author: User

Notice of the use of the online has a variety of summary, CSDN also have a lot of summary is very in place, do not do the summary of repetition, the need for students can search by themselves or refer to the link given below. began to study seriously read some, now the development of the function, the recent encounter some of their problems and ideas to share to everyone.


First, it is difficult to evade the de compatibility problem


1, the direct new Notification () This way is obsolete, and therefore did not go to scrutiny this way, the direct use of the new Notificationcompat.builder (context). Build (This is in support V4 package, the following content is all implemented in this way)

2. Compatibility of custom layouts

Using Notificationcampat for custom layouts We can do this:

Remoteviews Rvmain = new Remoteviews (Context.getpackagename (), r.layout.notification_layout),//TODO rvMain ... Notificationcompat.builder Builder = new Notificationcompat.builder (context). SetContent (Rvmain);//Tood ...
Above 4.0, this is no problem, but at 2.3, you will find that this is simply not valid, so we need to change the way:

Remoteviews Rvmain = new Remoteviews (Context.getpackagename (), r.layout.notification_layout),//TODO rmMain ... Notificationcompat.builder Builder = new Notificationcompat.builder (context). SetContent (Rvmain);//Tood ... Notification Notification = Builder.build (); if (Build.VERSION.SDK_INT <=) {notification.contentview = Rvmain;}

3. Action events on the notification bar

Let's talk about the notification events that we can handle:

Setcontentintent (): Triggered when a user clicks on a notification

Setfullscreenintent ()://todo This will be called when the notification is displayed.

Setdeleteintent (): Triggered when the user clears the notification, either by clicking the Clear button or by sliding the delete right or left (of course, if it is a higher version)

2.3 and below are not able to handle the action events in the custom layout, so we don't have to think about adding a custom button.


Ii. problems that may be encountered

1. Ticker icon shows only the middle part


New Notificationcompat.builder (context). Setsmallicon (R.drawable.ic_notice_small, 0)

We can set the icon to be displayed when ticker is in this way. When we tested the Xiaomi and Sony machines, the solution was to replace the previous large icon (we originally 72x72) with the current 32x32 small icon and move it from the previous xxhdpi to the hdpi folder.

2, ticker do not display: Direct display notification of the icon, the middle of the ticker process is not shown

This did not find what is the reason, directly put Setfullscreenintent (Mpifullscreen, false) This line can be written off

3, click the notification, the notification list does not disappear

This has only been encountered on one of Huawei's machines, and the solution is to send a closed broadcast after the Click event:

Sendbroadcast (New Intent (intent.action_close_system_dialogs));
Reference: Http://stackoverflow.com/questions/18261969/clicking-android-notification-actions-does-not-close-notification-drawer

4, there are all kinds of chaos

The confusion here is that the notifications are not displayed, multiple notifications are clicked to trigger the same events, multiple notifications are intent the same, and so on. At this time you may need to check the ID of the notification and Pendingintent's requestcode.

5, various models of the adaptation

This is quite chaotic, for example, mostly white on black, but some Huawei models are gray-bottom black words; some do not support setting the font color in custom layouts, some will mask the background color and transparency of custom layouts, and some may even be able to customize some of the layout of the notification bar (the icon on the left is not changed):


Once in pursuit of the custom layout of the left icon and the default notification of the left side of the icon width of the same size, do a lot of adaptation, such as a variety of dimens, but just fit, or can not support all models, then gave up. So far no perfect solution has been found, many of the best applications on the market do not very good, compared to the left side of the width of the icon you know. Recently found this feature in ink weather:

Is this a compromise of chaos?


Iii. Some extensions of customization

Our desires are increasing from the start of processing the action events in the notification to the realization of a higher notification than the existing one. Although the good things are always late, but for the moment, these ideas can be achieved.
The 4.1 default three big style can meet many of our needs, such as:

From left to right should be Bigtextstyle, Bigpicturestyle, Inboxstyle, this is not much to say, according to the demo can get out. The following is the Baidu Music player notification effect, in this simple implementation, the effect

Know how to do, in fact, is very easy to achieve, but the key is not know how to do. First see this effect, to try a variety of methods, such as specifying the height of the view, in different machines (mainly high version) of the machine running, found that no, Google, no results. What to do? Decompile it, found in the resource file Layout-v16 folder has a layout, this layout is our layout, in a lot of Java files found such a line of code:

Notification.bigcontentview = * * * *;
What do you think? See, it is the key sentence, look at the API, this property needs 16 and above to support, so we can write this when implemented:

if (Build.VERSION.SDK_INT >=) {notification.bigcontentview = Rvmain;}
Pay our own remoteview to Bigcontentview. When you realize it, you may find that this height is not what we arbitrarily specify, yes, it is, and his height is 100dp. This allows us to customize the RemoteView to achieve a variety of brilliant notifications.


Iv. inform the source of Notificationmanager

I would like to analyze the Notificationmanager, but found that his code is very few, as long as the completion of his own notification and then call its method can be. Concrete implementation is the bottom of the matter, but also to the soft rib

But simply browsing or discovering a little bit--and a toast-related, they all used the Inotificationmanager:

    Static private Inotificationmanager GetService () {        if (sservice! = null) {            return sservice;        }        Sservice = INotificationManager.Stub.asInterface (Servicemanager.getservice ("Notification"));        return sservice;    }

Toast's Show ():

    /**     * Show The View for the specified duration.     */Public    void Show () {        if (Mnextview = = null) {            throw new RuntimeException ("Setview must has been called"); c6/>}        Inotificationmanager service = GetService ();        String pkg = Mcontext.getpackagename ();        TN tn = MTN;        Tn.mnextview = Mnextview;        try {            Service.enqueuetoast (pkg, TN, mduration);        } catch (RemoteException e) {            //Empty        }    }
Notificatiomanager's Notify ():

    /** * Post a notification to being shown in the status bar. IF a notification with * the same tag and ID have already been posted by your application and have not yet been * CA     Nceled, it'll be replaced by the updated information.  * * @param tag A string identifier for this notification.     May is {@code null}.  * @param ID An identifier to this notification.     The pair (tag, id) must be unique * within your application. * @param notification A {@link notification} object describing what to * show the user.     Must not is null.        */public void notify (String tag, int id, Notification Notification) {int[] idout = new Int[1];        Inotificationmanager service = GetService ();        String pkg = Mcontext.getpackagename ();            if (notification.sound! = null) {Notification.sound = Notification.sound.getCanonicalUri (); if (strictmode.vmfileuriexposureenabled ()) {Notification.sound.checkFilEuriexposed ("Notification.sound");        }} if (LOCALLOGV) log.v (TAG, pkg + ": Notify (" + ID + "," + Notification + ")"); try {service.enqueuenotificationwithtag (pkg, mcontext.getoppackagename (), tag, id, Notifica            tion, Idout, Userhandle.myuserid ());            if (id! = idout[0]) {LOG.W (TAG, "Notify:id corrupted:sent" + ID + ", got Back" + idout[0]); }} catch (RemoteException e) {}}


Reference (Reference):

http://blog.csdn.net/vipzjyno1/article/details/25248021

http://blog.csdn.net/feng88724/article/details/6259071

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.