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 ...
随机推荐
- 蓝桥杯备战(一)3n+1问题
[问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该 ...
- TC SRM498 Div1 1000PT(容斥原理+DP)
[\(Description\)] 网格中每步可以走\((0,\cdots M_x,0\cdots M_y)\)中任意非零向量,有\(K\)种向量不能走,分别是\((r_1,r_1),(r_2,r_2 ...
- Redis学习笔记(七) 数据库
Redis 服务器将所有的数据库都保存在服务器状态redisServer结构的db数组中,db数组的每个项都是一个redisDB: struct redisServer{ //一个数组保存着服务器中的 ...
- python--常用模块calendar
常用模块: calendar.time.datetime.timeit.os.shutil.zip.math.string 上述所有的模块使用理论上都应该先导入,string是特例 -calendar ...
- ES6-10笔记(二)
class类 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类) ...
- 简单而面试中又常见的知识点:JS执行机制
在开始讲解之前,我们先来看一段代码: console.log('1'); setTimeout(function() { console.log('2'); process.nextTick( ...
- LinkedList源码(add方法)
对于要有扎实的java基础,集合是必须掌握的,而且精读这部分的源码很有用,也很有必要.而LinkedList是在java.util包下,和java.io,java.lang都是比较常用,而且比较简单. ...
- 【Django+Element UI】使用一个接口文件,搞定分页获取数据,模糊查询后分页获取数据
1:序列化获取数据的接口设计 1:分页获取序列化数据 2:是个能传参数的接口 class Society(APIView): def post(self, request): keywords = s ...
- html页面加载顺序
页面总是从上往下执行 CSS为什么要放在头部 1.CSS可以和html一起同时进行解析和渲染 2.如果你把CSS放到body后面,不但没有跟html一起进行加载渲染,还要花费额外时间去加载CSS,这样 ...
- linux DRM/KMS 测试工具 modetest、kmscude、igt-gpu-tools (一)
这里整理几个在学习Linux DRM/KMS中用到的几个工具,modetest.kmscude.igt-gpu-tools. 简介: modetest 是由libdrm提供的测试程序,可以查询显示设备 ...