Android Notification analysis-problems you may encounter, android
There are various summaries on the use of notifications on the Internet, and many summaries on csdn are also in place. Here we will not repeat the summary here. You can search for them or refer to the links below. When I started learning, I carefully read some of them. Now the function development is complete, and I will share some of my recent problems and experiences with you.
I. It is difficult to avoid de compatibility issues
1. The direct new Notification () method is out of date, so I did not elaborate on this method, and I directly used new NotificationCompat. builder (context ). build () (in the support v4 package, the following content is implemented in this way)
2. Compatibility with custom la s
You can use icationicationcampat to customize the layout as follows:
RemoteViews rvMain = new RemoteViews(context.getPackageName(), R.layout.notification_layout);//TODO rvMain...NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContent(rvMain);// TOOD ...
In the case of over 4.0, this is okay, but in the case of 2.3, you will find that this does not work at all, so we need to use another method:
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 <= 10){notification.contentView = rvMain;}
3. Operation events on the notification bar
Let's talk about the notification events we can handle:
SetContentIntent (): triggered when a user clicks a notification.
SetFullScreenIntent (): // TODO, which will be called when the notification is displayed.
SetDeleteIntent (): triggered when the user clears the notification. You can click the Clear button or slide the left or right to delete the notification (of course, the premise is that the high version)
2.3 and below cannot process the operation events in the custom layout, so we should not consider adding the Custom button.
Ii. Possible Problems
1. The Ticker icon displays only the middle part.
new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notice_small, 0)
We can set the icon displayed when Ticker. During our tests, Xiaomi and sony appeared on the machine. The solution was to replace the previous large icon (originally 72 × 72) with the current 32 × 32 small icon, and move it from the previous xxhdpi to the hdpi folder.
2. Ticker not displayed: the notification icon is displayed directly, but the Ticker process in the middle is not displayed.
The reason is not found. Just log out setFullScreenIntent (mPIFullScreen, false ).
3. Click notification. The notification list does not disappear.
This was only encountered on one Huawei machine. The solution was 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. Various chaos
Here, Chaos means that the notification is not displayed, the events triggered by multiple click notifications are the same, and the intents of multiple notifications are the same. In this case, you may need to check the notification ID and PendingIntent requestCode.
5. Adaptation of various models
This is quite confusing. For example, most of them are black and white, but some Huawei models are black and gray. Some do not support setting the font color in the custom layout; some will shield the background color and transparency of custom la S (for example, some vivo models), and some even you can only customize the la s of the notification bar (the icons on the Left cannot be changed, huawei ):
In the past, the icons on the left side of the custom Layout were the same width and size as the icons on the left side of the default notification. Many adaptations were made, such as various dimens. However, they are only compatible and still cannot support all models, then I gave up. No perfect solution has been found so far, and many excellent applications on the market are not doing very well. You will know the width of the icon on the left. This feature was recently found in moji weather:
Isn't that a compromise on chaos?
Iii. Custom extensions
From starting to process the operation events in the notification to implementing a notification that is higher than the existing notification, our desire is constantly increasing. Although good things always come late, we can all implement these ideas.
The default three big styles in 4.1 can meet our needs in many cases, such:
BigTextStyle, BigPictureStyle, and InboxStyle should be arranged from left to right. This is not much to mention. You can follow the demo. It is worth noting that by default, BigStyle is scaled down in parallel with both fingers, folding is slide-up (meizu is amazing, not like this ). The following is the notification Effect of Baidu music player.
Knowing how to do it is actually easy to implement, but the key is not knowing how to do it. First of all, I tried a variety of methods, such as specifying the view height and running it on different machines (mainly later versions, no results. What should I do? Decompilation, in the resource file found a layout under the layout-v16 folder, this layout is our layout, in a lot of java files find such a line of code:
notification.bigContentView = *****;
How is it? You understand ~~ It is the key sentence. To view the api, this attribute must be 16 or above. Therefore, we can write this statement during implementation:
if(Build.VERSION.SDK_INT >= 16){notification.bigContentView = rvMain;}
Pay our own RemoteView to bigContentView. In actual implementation, you may find that this height is not anything we specify. Yes, it is. Its height is 100dp. In this way, we can customize RemoteView to implement various brilliant notifications.
4. source code for notifying icationicationmanager
I wanted to analyze icationicationmanager, but found that there was very little code, as long as I completed my own Notification and then called its method. The specific implementation is the underlying things, but it's still a weakness.
However, we still find a little bit of association with Toast in simple browsing. They all use inoicationicationmanager:
static private INotificationManager getService() { if (sService != null) { return sService; } sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification")); return sService; }
Toast show ():
/** * Show the view for the specified duration. */ public void show() { if (mNextView == null) { throw new RuntimeException("setView must have been called"); } 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 be shown in the status bar. If a notification with * the same tag and id has already been posted by your application and has not yet been * canceled, it will be replaced by the updated information. * * @param tag A string identifier for this notification. May be {@code null}. * @param id An identifier for 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 be 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, notification, idOut, UserHandle.myUserId()); if (id != idOut[0]) { Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); } } catch (RemoteException e) { } }
Reference ):
Http://blog.csdn.net/vipzjyno1/article/details/25248021
Http://blog.csdn.net/feng88724/article/details/6259071
Demo: Download
We will teach you a question about android Notification,
If you read the source code of android, you will know that android notification is handled by icationicationservice, and all notifications are displayed in the drop-down list. The screen lock is another activity from the source, unless the phone skin supports notify, the default skin does not exist.
That is to say, the iphone effect cannot be imitated. However, if you have a specific user group, you can consider implementing a skin and integrating the application into it.
Notification Settings in android
Before you
Notification. defaults = Notification. DEFAULT_LIGHTS;
Later
Notification. defaults = Notification. DEFAULT_VIBRATE;
Therefore, DEFAULT_LIGHTS does not work.
The ringtone mode of a mobile phone is ringtone + no vibration. It should be changed to the ringtone + vibrate mode.