Android如何使用Notification进行通知
有两张图片素材会放在末尾
activity代码,和XML布局
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class NotificationActivity extends AppCompatActivity {
public static NotificationManager notificationManager;
public static String CHANNEL_1 = "channel1";
Notification notification;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
iv = findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.setImageResource(R.drawable.aoligei);
notificationManager = (NotificationManager) NotificationActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
//android 8.0有通知渠道,如果不设置的话,会报错
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
////配置通知渠道id,渠道名称(用户可以看到),渠道优先级
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1, "通知渠道1", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("通知渠道的描述1");
///配置通知出现时的闪灯(如果 android 设备支持的话)
channel1.enableLights(true);
channel1.setLightColor(Color.WHITE);
//配置通知出现时的震动(如果 android 设备支持的话)
channel1.enableVibration(true);
channel1.setVibrationPattern(new long[]{100, 200, 100, 200});
//channel.setBypassDnd(true); //设置绕过免打扰模式
//channel.canBypassDnd(); //检测是否绕过免打扰模式
//channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知
notificationManager.createNotificationChannel(channel1);
// NotificationChannel channel = new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
// channel.setShowBadge(true);
// NotificationManager notificationManager = (NotificationManager) getSystemService(
// NOTIFICATION_SERVICE);
// notificationManager.createNotificationChannel(channel);
}
//普通的通知
// sendChatMsg();
//可以点击的通知
preIntent();
}
});
}
public void sendChatMsg() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1)
.setContentTitle("收到一条聊天消息")
.setContentText("老八问候你吃了吗")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setAutoCancel(true)
.setNumber(2)
.build();
// for (int i = 0; i < 3; i++) {
manager.notify(1, notification);
// }
}
public void preIntent(){
NotificationManager manager1 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建一个可以点击的通知
//通知的需要跳转,创建Intent
Intent intent = new Intent(NotificationActivity.this, NotificationActivity.class);
//需要创建PendingIntent进行
PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
//创建notification
notification = new Notification.Builder(NotificationActivity.this,CHANNEL_1).//第二个参数需要保持一致
setContentTitle("这是一个通知") .setContentText("快来和老八一起共进晚餐") .
setWhen(System.currentTimeMillis()) .
setSmallIcon(R.mipmap.ic_launcher) .
setLargeIcon(BitmapFactory.decodeResource( getResources(), R.mipmap.ic_launcher)) .
setContentIntent(pendingIntent).
build();
//。build就是创建的意思
//这个id是随便起的名字,只要id不同可以多个显示,如果id相同,会显示玩当前id的通知才会显示下一个id
manager1.notify(0x11, notification);
}
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/haha"
/>
<!-- android:adjustViewBounds="true"-->
<!-- android:scaleType="fitXY"-->
</LinearLayout>


Android如何使用Notification进行通知的更多相关文章
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- Android学习:Notification状态栏通知
Notification是显示在手机状态栏的通知,它代表一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification.在小米手机上,手指在屏幕顶端向下划 ...
- 在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android学习总结(十五) ———— Notification(状态栏通知)基本用法
一.Notification基本概念 Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...
- Android Notification 消息通知 相关资料.md
目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...
- Android中使用Notification实现进度通知栏(Notification示例三)
我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...
- Android中使用Notification实现宽视图通知栏(Notification示例二)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- Android 和iOS 创建本地通知
1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...
随机推荐
- Win32 Sdk 连接Access数据库
/************************************************************* *** MyWinClass.cpp 创建窗口模板 *** vs2017+ ...
- React-Router 4 两个常用路由变量
讲真我个人不太喜欢4.x版本,虽然作者自信动态路由的形式符合React组件化的哲学,但是路由和一般组件耦合太深,而且后期组件分片也麻烦,以后需要重构的话怕是会一番折腾.同学公司用的还是3.x版本. 不 ...
- Aangular 父子间组件传递
1.父子间组件传递------重点&难点 Vue.js和Angular中的父子间消息传递原理一样,都可以用口诀: “Props Down,Events Up” 方向1:父 =>子 父组件 ...
- 3.2 Go整数类型
1. Go整数类型 Go语言的数值类型包含不同大小的整数型.浮点数和负数,每种数值类型都有大小范围以及正负符号. 官方文档解释数据类型 int类型中哪些支持负数 有符号(负号):int8 int16 ...
- J2EE项目中,servlet跳转到相应的JSP页面后,JSP页面丢失了样式效果
原因: js和css的引用路径是相对路径.跳转后路径改变. 解决方法: 先在head标签中加入一下代码 <% String path = request.getContextPath(); St ...
- poi--读取不同类型的excel表格
要想根据不同类型excel表格获取其数据,就要先判断其表格类型 poi-api种方法: getCellType public int getCellType() Return th ...
- js操作html的基本方法
刚学了js操作html的基本方法,在写代码过程中,有很多格式不规范,忘记加双引号尤其重要,通常这 样的错误很容易范,并且这种错误很难找.随着代码学习量越来越多,很多写法容易搞混.今天记录一下,以便后期 ...
- iptables基本用法
iptables选项参数 [root@test ~]# iptables --help -L #列出指定表所有链上的所有规则,本选项须置于-n选项后面 -n #以数字格式显示地址和端口号 -v #详细 ...
- SpringBoot外部化配置使用Plus版
本文如有任何纰漏.错误,请不吝指正! PS: 之前写过一篇关于SpringBoo中使用配置文件的一些姿势,不过嘛,有句话(我)说的好:曾见小桥流水,未睹观音坐莲!所以再写一篇增强版,以便记录. 序言 ...
- Spring笔记 - 组件注册
@Bean:类注入容器 xml方式: <bean id="person" class="com.hrh.bean.Person"> <prop ...