文章目录

  • 1. 配置
    • 1.1 环境
    • 1.2 参考博客
  • 2. 上代码
    • 2.1 逻辑代码
    • 2.2 权限代码

1. 配置

1.1 环境

环境:// 2020-12-08
AndroidO (8以上)
Android Studio 4.0.1

1.2 参考博客

此处只列出自己感觉有用的博客,供大家参考。赠人玫瑰,手有余香。
1. 关于android5.0以上,notification横幅通知(setFullScreenIntent)弹出程序主页面问题。
https://blog.csdn.net/csdn_lwp/article/details/54576071
2. Android基础知识(二十):Notification、提醒式通知(横幅)踩坑与通知界面设置跳转
https://blog.csdn.net/weixin_38196407/article/details/89556023(权限设置)
3. Android8.0 通知栏不显示、无横幅通知问题解决
https://blog.csdn.net/mr_leixiansheng/article/details/84942589(权限设置)
4. Android通知栏前台服务
https://www.cnblogs.com/jqnl/p/12599905.html

2. 上代码

2.1 逻辑代码

  • service.java
/**提示代码*/
private Notification createForegroundNotification() {NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 唯一的通知通道的id.String notificationChannelId = "notification_channel_id_01";// Android8.0以上的系统,新建消息通道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//用户可见的通道名称String channelName = "Foreground Service Notification";//通道的重要程度int importance = NotificationManager.IMPORTANCE_HIGH;NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);notificationChannel.setDescription("Channel description");//LED灯notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);//震动notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});notificationChannel.enableVibration(true);// 声音(没有声音)notificationChannel.setSound(null, null);if (notificationManager != null) {notificationManager.createNotificationChannel(notificationChannel);}}NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannelId);builder.setCategory(Notification.CATEGORY_RECOMMENDATION);//通知小图标builder.setSmallIcon(R.drawable.ic_launcher_foreground);//通知标题builder.setContentTitle("NotificationTitle");//通知内容builder.setContentText("ContentText");//设定通知显示的时间builder.setWhen(System.currentTimeMillis());//设定启动的内容Intent activityIntent = new Intent(this, ForegroundSerActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent);// sdk5.0以上使用builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);builder.setFullScreenIntent(pendingIntent, false);//创建通知并返回return builder.build();}

2.2 权限代码

  • AndroidManifest.xml
<!--前台服务开启权限-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!--弹出横幅通知的权限-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
  • Activity.java
 /**跳转横幅通知权限,详细channelId授予权限*/private void getHangUpPermission(String channelId) {Intent intent = new Intent();if (Build.VERSION.SDK_INT >= 26) {// android8.0单个channelid设置intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);} else {// android 5.0以上一起设置intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);intent.putExtra("app_package", getPackageName());intent.putExtra("app_uid", getApplicationInfo().uid);}startActivity(intent);}

感想:Android的权限逐渐向着用户倾斜,开发人员需要更加提高自身素质。
此外:通知相关权限必须要用户手动开启。
最后的最后:这是我一天的成果,希望各位看官提供宝贵建议。没有建议的点个赞也好呗。

AndroidO Notification横幅通知(HangUp Notification)相关推荐

  1. Android Notification 手机系统横幅弹出提示框调用,横幅通知,RemoteViews使用实例

    直接上代码 :bundle是极光推送的bundle @Override public void onReceive(Context context, Intent intent) {try {Bund ...

  2. android Foreground Service 前台服务/notification全局通知

    android Foreground Service 前台服务/notification全局通知 前言 要素简介 前台服务(Foreground Service) 全局通知(notification) ...

  3. iOS 远程通知(Remote Notification)和本地通知(Local Notification)

    ios通知分为远程通知和本地通知,远程通知需要连接网络,本地通知是不需要的,不管用户是打开应用还是关闭应用,我们的通知都会发出,并被客户端收到 我们使用远程通知主要是随时更新最新的数据给用户,使用本地 ...

  4. [html] html5的Notification桌面通知如何请求权限?

    [html] html5的Notification桌面通知如何请求权限? Notification.requestPermission(callback); 个人简介 我是歌谣,欢迎和大家一起交流前后 ...

  5. Android学习—Notification消息通知

    最近在项目中需要使用消息通知,自己把它封装成了一个方法,需要的时候方便调用, 下面对Notification类中的一些常量,字段,方法简单介绍一下: 常量: DEFAULT_ALL    使用所有默认 ...

  6. android 通知传值,Android消息通知(notification)和PendingIntent传值

    Android支持Toast和NotificationManager两种通知方式,前者相当于一个定时关闭的对话框,后者是在状态栏上显示一条消息.Toast和Notification都可以随时取消. T ...

  7. HTML5中的Web Notification桌面通知(右下角提示)

    html5桌面通知(Web Notifications)对于需要实现在新消息入线时,有桌面通知效果的情况下非常有用,在此简单介绍一下这个html5的新属性.通过Web Notifications(桌面 ...

  8. Android状态栏语言,Android实现3种Notification(状态栏通知)

    Android实现3种Notification(状态栏通知) 点击标题下「安卓干货铺」可快速关注 Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示.当 APP 向系统发出通 ...

  9. 最好用的 6 款 Vue 实时消息提示通知(Message/Notification)组件推荐与测评

    本文完整版:<最好用的 6 款 Vue 实时消息提示通知(Message/Notification)组件推荐与测评> Vue 实时消息提示通知 Vue-notification - 专注实 ...

最新文章

  1. ARMv8/ARMv9指令集概述
  2. jQuery length 和 size()区别
  3. c语言 字符串 url,如何对URL字符串进行百分号编码
  4. git log 你学废了吗?
  5. Memcache的分布式应用
  6. win10iot 编程_使用DIY Blynk板对IoT按钮进行编程
  7. Visual Studio 安装OpenCV及问题总结
  8. 线程演示生产者和消费者的一个例子
  9. android java静态库,Android NDK开发相关知识集合
  10. HBase 实战(1)--HBase的数据导入方式
  11. C4—Qt实现记事本(一)2021-11-16
  12. 暴风一号 病毒 源码学习
  13. 【python基础】h5py库的基本使用
  14. LightDM详细介绍
  15. 品牌对比 | 佰草集 VS 膜法世家
  16. Android MediaRecorder问题:StartFailed解决方案
  17. 今日错题(10.6)
  18. oracle10G安装与配置
  19. python go 的区别
  20. Acala与全球知名跨链资产流动平台Ren达成合作

热门文章

  1. maya python插件_Maya Python - ALembic导入导出助手
  2. 简单模拟struts框架,了解strusts的框架实现机制
  3. Linux TC 流量控制与排队规则 qdisc 树型结构详解(以HTB和RED为例)
  4. 安装pyltp模块详细教程(windows下python安装)
  5. 从西刺代理爬取代理ip,并验证是否可用
  6. 计算机磁盘为uefi引导,科普一分钟|UEFI引导+GPT硬盘格式装系统方法
  7. 云计算上安装mysql,redis
  8. 解决linux对D盘(非系统盘)只有只读权限,无法创建文件夹 (linux 重新挂载盘符)
  9. Google翻译接口(PHP API)
  10. 什么是软件生命周期模型?试比较瀑布模型、快速原型模型、增量模型和螺旋模型的优缺点,说明每种模型的使用范围。