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 ...
随机推荐
- SORM框架01
架构图 Query接口:负责查询(对外提供的核心服务类) QueryFactory类:负责根据配置信息创建Query对象 TypeConvertor接口:类型转换 TableContext类:负责获取 ...
- java 8 stream、lambda表达式对list操作分组、过滤、求和、最值、排序、去重
1.分组 通过groupingBy分组指定字段 list.stream().collect(Collectors.groupingBy(User::getSex)); 2.过滤 通过filter方法过 ...
- 多线程高并发编程(9) -- CopyOnWrite写入时复制
CopyOnWrite写入时复制 CopyOnWrite,即快照模式,写入时复制就是不同线程访问同一资源的时候,会获取相同的指针指向这个资源,只有在写操作,才会去复制一份新的数据,然后新的数据在被写操 ...
- 《 .NET并发编程实战》一书中的节流为什么不翻译成限流
有读者问,为什么< .NET并发编程实战>一书中的节流为什么不翻译成限流? 这个问题问得十分好!毕竟“限流”这个词名气很大,耳熟能详,知名度比“节流”大多了. 首先,节流的原词Thrott ...
- TC SRM498 Div1 1000PT(容斥原理+DP)
[\(Description\)] 网格中每步可以走\((0,\cdots M_x,0\cdots M_y)\)中任意非零向量,有\(K\)种向量不能走,分别是\((r_1,r_1),(r_2,r_2 ...
- 改进Page Object公共方法封装(base_page)
import os from time import sleep from selenium import webdriver from selenium.webdriver.common.by im ...
- Docker之从零开始制作docker镜像
以前学习docker是直接docker pull命令直接拉取Linux中已有镜像,并创建容器,添加应用程序,但是docker镜像一开始是怎么来的呢?下面将从零开始介绍整个docker镜像的制作过程(初 ...
- vue+express上传头像到数据库中img的路径
项目结构 express中间件指定静态资源目录 app.use("/static",express.static(path.join(__dirname,"/public ...
- JDBC基本使用方法
JDBC基本使用方法 JDBC固定步骤: 加载驱动 String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true& ...
- 必须返回对象时,别妄想返回其reference 【Effective C++ 条款21】
class Rational { public: Rational(, ) : n(numerator), d(denominator) { printf("Rational Constru ...