Heads Up Notification (setFullScreenIntent not work on Mate8)
2017-10-13 10:04
726 查看
本文仅记录在华为Mate8(Android 7.0)上Heads Up Notification的实现代码。至于为什么这么实现,由于时间缘故,原因尚未找到。如有同学指点,万分荣幸!
在Android5.0后,Notification开始支持Heads Up模式。该模式下,不需要下拉通知栏就直接显示出来 ,悬挂在屏幕上方,并且焦点不变,仍在用户操作的界面。因此,不会打断用户的操作,通知过几秒就会自动消失。最典型的应用是,微信的聊天消息通知。网络上对于这种模式的叫法很多,类似横幅通知,悬挂式通知等。
网上对于实现效果众说纷纭,大多倾向于调用NotificationCompat.Builder的方法
但在不同机器上,似乎实现效果也不尽相同。
在华为Mate8(Android 7.0)上,调用setFullScreenIntent反而无法实现Heads Up。
最终注掉setFullScreenIntent。核心代码如下。
使用Heads Up Notification也有必要前提条件。
(1)Android API >=21,即5.0以上
(2)需要在设置中开启横幅通知权限(在设置通知管理中,不同机型可能存在差异)
(3)需要设置震动或声音。(必须!)
(4)需要设置Notification为最高权限。(必须!)
Android对于Notification的设计,个人感觉比较乱。在3.0前后的实现方式不同,且存在很多隐藏的必要条件未说明,可能是需要向前兼容的需求吧。
比如,普通的Notification有大小图标的设置。若不设置大图标,系统会自动使用APP图标,但如果不设置小图标,Notificatin将无法显示。
在解决问题过程中看过的LOG,增益良多,这里也一并记录。
《Heads up 让通知弹出来》http://blog.csdn.net/a376051132/article/details/61193102
《Android Notification 样式》https://www.2cto.com/kf/201605/510102.html
《android 特殊用户通知用法汇总–Notification源码分析》http://blog.csdn.net/self_study/article/details/51055769
Demo推荐github上的https://github.com/zhaozepeng/notification
需要特别吐槽的是,下面这篇文章。花了我3点CSDN积分下载DEMO,但DEMO中对notification未设置最高权限等级,未设置声音/震动,致使Heads Up效果无法展现
《Android5.x Notification应用解析》http://blog.csdn.net/itachi85/article/details/50096609
在Android5.0后,Notification开始支持Heads Up模式。该模式下,不需要下拉通知栏就直接显示出来 ,悬挂在屏幕上方,并且焦点不变,仍在用户操作的界面。因此,不会打断用户的操作,通知过几秒就会自动消失。最典型的应用是,微信的聊天消息通知。网络上对于这种模式的叫法很多,类似横幅通知,悬挂式通知等。
网上对于实现效果众说纷纭,大多倾向于调用NotificationCompat.Builder的方法
public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority)
但在不同机器上,似乎实现效果也不尽相同。
在华为Mate8(Android 7.0)上,调用setFullScreenIntent反而无法实现Heads Up。
最终注掉setFullScreenIntent。核心代码如下。
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); builder.setContentIntent(pendingIntent);//设置pendingIntent builder.setSmallIcon(R.drawable.foldleft); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher)); builder.setAutoCancel(true); builder.setContentTitle("悬挂式通知"); builder.setPriority(Notification.PRIORITY_MAX);//设置最高权限 builder.setDefaults(Notification.DEFAULT_ALL);//设置声音和震动 notificationManager.notify(2, builder.build());
使用Heads Up Notification也有必要前提条件。
(1)Android API >=21,即5.0以上
(2)需要在设置中开启横幅通知权限(在设置通知管理中,不同机型可能存在差异)
(3)需要设置震动或声音。(必须!)
(4)需要设置Notification为最高权限。(必须!)
Android对于Notification的设计,个人感觉比较乱。在3.0前后的实现方式不同,且存在很多隐藏的必要条件未说明,可能是需要向前兼容的需求吧。
比如,普通的Notification有大小图标的设置。若不设置大图标,系统会自动使用APP图标,但如果不设置小图标,Notificatin将无法显示。
在解决问题过程中看过的LOG,增益良多,这里也一并记录。
《Heads up 让通知弹出来》http://blog.csdn.net/a376051132/article/details/61193102
《Android Notification 样式》https://www.2cto.com/kf/201605/510102.html
《android 特殊用户通知用法汇总–Notification源码分析》http://blog.csdn.net/self_study/article/details/51055769
Demo推荐github上的https://github.com/zhaozepeng/notification
需要特别吐槽的是,下面这篇文章。花了我3点CSDN积分下载DEMO,但DEMO中对notification未设置最高权限等级,未设置声音/震动,致使Heads Up效果无法展现
《Android5.x Notification应用解析》http://blog.csdn.net/itachi85/article/details/50096609
相关文章推荐
- Set up Shared Screen on Linux
- get a full screen popup to work on WP8
- Error:setLatestEventInfo(AlarmAlertFullScreen,String,String,PendingIntent)(182, 10) 错误: 找不到符号 符号:
- [ WARN ] Keyword 'Capture Page Screenshot' could not be runon failure: No browser isopen
- Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s) 解决方法
- How To Set Up Apache Virtual Hosts on Ubuntu 14.04 LTS
- Set Up Scheduler Job on Azure
- VCSA 6.0提示quick stats on hostname is not up-to-date解决办法
- Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_OFF
- 转载:How (not) to set a timeout on a computation in Python
- Instant Run performed a full build and install since the installation on the device does not match t
- Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s) 解决方法
- Android OnSensorChanged() not working when screen lock is on
- How to Set Up SSL on IIS 7
- CentOS6 在VirtualBox中实现全屏和根据窗口大小自动调整分辨率 | Install Guest additions and run fullscreen mode on VirtualB
- Set up of Qt/Embedded 4.5 on ubuntu 9.04
- Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s) 解决方法
- Laravel报错:Session store not set on request.
- How to set up a Subversion (SVN) server on CentOS or Fedora