需要全部源码或运行有问题请点赞关注收藏后评论区留言~~~

一、通知渠道NtoificationChannel

为了分清消息通知的轻重缓急,Android8.0新增了通知渠道,并且必须指定通知渠道才能正常推送消息,一个应用允许拥有多个通知渠道,每个渠道的重要性各不相同,有的渠道消息在通知栏被折叠成小行,有的渠道消息在通知栏展示完整的大行,有的会发出铃声甚至震动等等

效果如下 可以输入标题和内容,然后下拉框里面选择消息的重要级别,根据重要级别的不同消息会有不同的通知方式(这里要连接真机才能具体展示 读者可自行连接 或者参考我之前的博客连接步骤)

代码如下

Java类

package com.example.chapter11;import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import com.example.chapter11.util.NotifyUtil;
import com.example.chapter11.util.ViewUtil;public class NotifyChannelActivity extends AppCompatActivity implements View.OnClickListener {private EditText et_title;private EditText et_message;private String mChannelId = "0"; // 通知渠道的编号private String mChannelName; // 通知渠道的名称private int mImportance; // 通知渠道的级别@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notify_channel);et_title = findViewById(R.id.et_title);et_message = findViewById(R.id.et_message);findViewById(R.id.btn_send_channel).setOnClickListener(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {initImportanceSpinner(); // 初始化渠道级别的下拉框}}// 初始化渠道级别的下拉框private void initImportanceSpinner() {findViewById(R.id.ll_channel).setVisibility(View.VISIBLE);ArrayAdapter<String> importanceAdapter = new ArrayAdapter<String>(this,R.layout.item_select, importanceDescArray);Spinner sp_importance = findViewById(R.id.sp_importance);sp_importance.setPrompt("请选择渠道级别");sp_importance.setAdapter(importanceAdapter);sp_importance.setSelection(3);sp_importance.setOnItemSelectedListener(new TypeSelectedListener());}private int[] importanceTypeArray = {NotificationManager.IMPORTANCE_NONE,NotificationManager.IMPORTANCE_MIN,NotificationManager.IMPORTANCE_LOW,NotificationManager.IMPORTANCE_DEFAULT,NotificationManager.IMPORTANCE_HIGH,NotificationManager.IMPORTANCE_MAX};private String[] importanceDescArray = {"不重要", // 无通知"最小级别", // 通知栏折叠,无提示声音,无锁屏通知"有点重要", // 通知栏展开,无提示声音,有锁屏通知"一般重要", // 通知栏展开,有提示声音,有锁屏通知"非常重要", // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)"最高级别" // 通知栏展开,有提示声音,有锁屏通知,在屏幕顶部短暂悬浮(有的手机需要在设置页面开启横幅)};class TypeSelectedListener implements AdapterView.OnItemSelectedListener {public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {mImportance = importanceTypeArray[arg2];mChannelId = "" + arg2;mChannelName = importanceDescArray[arg2];}public void onNothingSelected(AdapterView<?> arg0) {}}// 发送指定渠道的通知消息(包括消息标题和消息内容)private void sendChannelNotify(String title, String message) {// 创建一个跳转到活动页面的意图Intent clickIntent = new Intent(this, MainActivity.class);// 创建一个用于页面跳转的延迟意图PendingIntent contentIntent = PendingIntent.getActivity(this,R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 创建一个通知消息的建造器Notification.Builder builder = new Notification.Builder(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// Android 8.0开始必须给每个通知分配对应的渠道builder = new Notification.Builder(this, mChannelId);}builder.setContentIntent(contentIntent) // 设置内容的点击意图.setAutoCancel(true) // 点击通知栏后是否自动清除该通知.setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标.setContentTitle(title) // 设置通知栏里面的标题文本.setContentText(message); // 设置通知栏里面的内容文本Notification notify = builder.build(); // 根据通知建造器构建一个通知对象if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);}// 从系统服务中获取通知管理器NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息,多条通知需要指定不同的通知编号notifyMgr.notify(Integer.parseInt(mChannelId), notify);if (mImportance != NotificationManager.IMPORTANCE_NONE) {Toast.makeText(this, "已发送渠道消息", Toast.LENGTH_SHORT).show();}}@Overridepublic void onClick(View v) {if (v.getId() == R.id.btn_send_channel) {ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘if (TextUtils.isEmpty(et_title.getText())) {Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(et_message.getText())) {Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();return;}// 发送指定渠道的通知消息(包括消息标题和消息内容)sendChannelNotify(et_title.getText().toString(), et_message.getText().toString());}}
}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息标题:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_title"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息标题"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息内容:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_message"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="top"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息内容"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_channel"android:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"android:visibility="gone"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="渠道级别:"android:textColor="@color/black"android:textSize="17sp" /><Spinnerandroid:id="@+id/sp_importance"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_margin="5dp"android:gravity="center"android:spinnerMode="dialog" /></LinearLayout><Buttonandroid:id="@+id/btn_send_channel"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送渠道消息"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout>

二、给桌面应用添加信息角标(以华为和小米为例)

每个应用都可以给用户发送很多消息,通知栏显然有时候不够容纳如此之多的消息,于是各应用希望向用户展现未读消息的数量,好让用户知晓有没有未读消息或者是有几条未读消息。例如微信的99+,几个小红点好有申请之类。

因为各手机厂商对消息角标的实现方案各不相同,因此只能给它们的手机分别适配处理,

下面依次介绍华为和小米系手机的适配编码

华为的消息角标不依赖通知推送,允许单独设置红点的展示情况

小米的消息角标方案则依赖于通知推送,必须在发送通知之时一起传送消息数量参数

效果如下 可自行设置

代码如下

Java类

package com.example.chapter11;import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import com.example.chapter11.util.NotifyUtil;
import com.example.chapter11.util.ViewUtil;public class NotifyMarkerActivity extends AppCompatActivity implements View.OnClickListener {private EditText et_title;private EditText et_message;private EditText et_count;private static String mChannelId = "3"; // 通知渠道的编号private static String mChannelName = "一般重要"; // 通知渠道的名称private static int mImportance = NotificationManager.IMPORTANCE_DEFAULT; // 通知渠道的级别@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notify_marker);et_title = findViewById(R.id.et_title);et_message = findViewById(R.id.et_message);et_count = findViewById(R.id.et_count);findViewById(R.id.btn_show_marker).setOnClickListener(this);findViewById(R.id.btn_clear_marker).setOnClickListener(this);}@Overridepublic void onClick(View v) {ViewUtil.hideOneInputMethod(this, et_message); // 隐藏输入法软键盘if (TextUtils.isEmpty(et_title.getText())) {Toast.makeText(this, "请填写消息标题", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(et_message.getText())) {Toast.makeText(this, "请填写消息内容", Toast.LENGTH_SHORT).show();return;}if (TextUtils.isEmpty(et_count.getText())) {Toast.makeText(this, "请填写消息数量", Toast.LENGTH_SHORT).show();return;}String title = et_title.getText().toString();String message = et_message.getText().toString();int count = Integer.parseInt(et_count.getText().toString());if (v.getId() == R.id.btn_show_marker) {sendChannelNotify(title, message, count); // 发送指定渠道的通知消息Toast.makeText(this, "已显示消息角标,请回到桌面查看", Toast.LENGTH_SHORT).show();} else if (v.getId() == R.id.btn_clear_marker) {sendChannelNotify(title, message, 0); // 发送指定渠道的通知消息Toast.makeText(this, "已清除消息角标,请回到桌面查看", Toast.LENGTH_SHORT).show();}}// 发送指定渠道的通知消息(包括消息标题和消息内容)private void sendChannelNotify(String title, String message, int count) {// 创建一个跳转到活动页面的意图Intent clickIntent = new Intent(this, MainActivity.class);// 创建一个用于页面跳转的延迟意图PendingIntent contentIntent = PendingIntent.getActivity(this,R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);// 创建一个通知消息的建造器Notification.Builder builder = new Notification.Builder(this);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {// Android 8.0开始必须给每个通知分配对应的渠道builder = new Notification.Builder(this, mChannelId);}builder.setContentIntent(contentIntent) // 设置内容的点击意图.setAutoCancel(true) // 点击通知栏后是否自动清除该通知.setSmallIcon(R.mipmap.ic_launcher) // 设置应用名称左边的小图标.setContentTitle(title) // 设置通知栏里面的标题文本.setContentText(message); // 设置通知栏里面的内容文本Notification notify = builder.build(); // 根据通知建造器构建一个通知对象if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotifyUtil.createNotifyChannel(this, mChannelId, mChannelName, mImportance);}NotifyUtil.showMarkerCount(this, count, notify); // 在桌面的应用图标右上方显示指定数字的消息角标// 从系统服务中获取通知管理器NotificationManager notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 使用通知管理器推送通知,然后在手机的通知栏就会看到该消息,多条通知需要指定不同的通知编号notifyMgr.notify(Integer.parseInt(mChannelId), notify);}}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="5dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="本页面的消息角标仅支持华为与小米手机"android:textColor="@color/black"android:textSize="17sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息标题:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_title"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息标题"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="70dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息内容:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_message"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="top"android:layout_margin="5dp"android:background="@drawable/editext_selector"android:hint="请填写消息内容"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="消息数量:"android:textColor="@color/black"android:textSize="17sp" /><EditTextandroid:id="@+id/et_count"android:layout_width="0dp"android:layout_height="match_parent"android:layout_margin="5dp"android:layout_weight="1"android:background="@drawable/editext_selector"android:gravity="left|center"android:inputType="number"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout><Buttonandroid:id="@+id/btn_show_marker"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="发送消息同时显示桌面角标"android:textColor="@color/black"android:textSize="17sp" /><Buttonandroid:id="@+id/btn_clear_marker"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="清除应用的消息角标"android:textColor="@color/black"android:textSize="17sp" /></LinearLayout>

创作不易 觉得有帮助请点赞关注收藏

Android Studio App开发之通知渠道NotificationChannel及给华为、小米手机桌面应用添加消息数量角标实战(包括消息重要级别的设置 附源码)相关推荐

  1. Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)

    运行有问题或需要全部源码请点赞关注收藏后评论区留言~~~ 一.循环视图RecyclerView 尽管ListView和GridView分别实现了多行单列和多行多列的列表,使用也很简单,可是它们缺少变化 ...

  2. Android Studio App开发之网络通信中使用POST方式调用HTTP接口实现应用更新功能(附源码 超详细必看)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ 一.POST方式调用HTTP接口 POST方式把接口地址与请求报文分开,允许使用自定义的报文格式,由此扩大了该方式的应用场景.POST请求与GET ...

  3. Android Studio App开发之下载管理器DownloadManager中显示、轮询下载进度、利用POST上传文件讲解及实战(附源码)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ 一.在通知栏显示下载进度 利用GET方式读取数据有很多缺点比如1:无法端点续传 一旦中途失败只能重新获取 2:不是真正意义上的下载操作 无法设置参 ...

  4. Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ 一.使用录音机录制音频 手机有自带的系统相机,也有自带的系统录音机,只要在调用startActivityForResult之前指定该动作,就会自动 ...

  5. Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)

     运行有问题或需要源码请点赞关注收藏后评论区留言~~ 显示Intent和隐式Intent Intent是各个组件之间的信息沟通的桥梁,既能在Activity之间沟通,又能在Activity与Servi ...

  6. Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)

    需要源码和图片集请点赞关注收藏后评论区留言~~~ 一.图像解码器ImageDecoder 早期的Android只支持3种图像格式,分别是JPEG,PNG,GIF 虽然这三类图片都能在ImageView ...

  7. Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ 一.下拉框Spinner Spinner是下拉框控件,它用于从一串列表中选择某项,其功能类似于单选按钮的组合,下拉列表的展示方式有两种,一种是在当 ...

  8. Android Studio App开发之使用摄像机录制视频和从视频库中选取视频的讲解及实战(附源码)

    运行有问题或需要源码请点赞关注收藏后评论区留言~~~ 一.使用摄像机录制视频 与音频类似,通过系统摄像机可以很方便的录制视频,只要指定摄像动作为MediaStore.ACTION_VIDEO_CAPT ...

  9. Android Studio App开发入门之选择按钮的讲解及使用(包括复选框,开关按钮,单选按钮,附源码)

    运行有问题或需要图片资源请点赞关注收藏后评论区留言~~~ 在学习复选框之前,先了解一下CompoundButton,在Android体系中,CompoundButton类是抽象的复合按钮,因为是抽象类 ...

最新文章

  1. HTTP系列之:HTTP缓存
  2. Effective Java之用实例域代替序数(三十一)
  3. 解决HTML embed标签显示在div上层(not z-index)
  4. Centos7搭建Ngrok
  5. javaweb中mysql数据库的回滚操作代码
  6. 用JS实现简单的省市联动
  7. oracle脚本转mpp脚本,范本:使用expdp/impdp克隆生成一个新数据库
  8. 预处理_关于食材预处理
  9. 前端如何来做权限管理?
  10. 打飞机小游戏c++窗口实现版
  11. 我们管理20人团队的方法
  12. 在一个线程中 sleep(1000) 方法,将使得该线程在多少时间后获得对 CPU 的控制(假设睡眠过程中不会有其他时间唤醒该线程)?
  13. 一文讲透,技术人如何快速提升沟通能力?
  14. Windows的功能键介绍(很全)
  15. 带轮轮毂长度l和带轮宽b表_B型V带轮的轮缘宽B_轮毂孔径D和轮毂长L.doc
  16. Linux 安装 JDK + Tomcat + Mysql
  17. 计算机主机后面板 图解,计算机主板揭秘(下)图文并茂版
  18. Linux内核机制总结进程管理之SMP调度(六)
  19. OPENWRT系统学习系列之一(系统源码到编译固件和烧录固件)
  20. 有效提高效率的PDF小技巧,你确定不了解下嘛?

热门文章

  1. 下载文件出现提示框或者直接显示在浏览器中
  2. linux添加超级用户
  3. chrome扩展- 天猫、淘宝、拼多多、1688 下单开发心得
  4. 咖啡技能知识培训|咖啡冲煮有哪几个基础要素
  5. 梅科尔工作室-MySQL学习笔记
  6. 集成电路CAD课程实验报告:PMOS和NMOS管的版图设计与IV特性仿真
  7. 爬取网易云音乐歌曲评论并生成特定词云图
  8. hybris Developer
  9. HisiPHP V2 后台管理框架源码
  10. 蓝桥杯———数字三角形(JAVA)