有两张图片素材会放在末尾

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进行通知的更多相关文章

  1. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  2. Android学习:Notification状态栏通知

    Notification是显示在手机状态栏的通知,它代表一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification.在小米手机上,手指在屏幕顶端向下划 ...

  3. 在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  4. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  5. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  6. Android Notification 消息通知 相关资料.md

    目录 Android Notification 消息通知 相关资料 Android 5.0 Lollipop (API 21)无法正常显示通知图标,只能看到一个白色方块或灰色方块的问题 解决方案 参考 ...

  7. Android中使用Notification实现进度通知栏(Notification示例三)

    我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...

  8. Android中使用Notification实现宽视图通知栏(Notification示例二)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

  9. Android 和iOS 创建本地通知

    1 Android 中的发送本地通知的逻辑如下 先实例化Notification.Builder,再用builder创建出具体的Notification,创建时要指定好启动用的PendingInten ...

随机推荐

  1. YOLOV4在linux下训练自己数据集(亲测成功)

    最近推出了yolo-v4我也准备试着跑跑实验看看效果,看看大神的最新操作 这里不做打标签工作和配置cuda工作,需要的可以分别百度搜索   VOC格式数据集制作,cuda和cudnn配置 我们直接利用 ...

  2. CentOS7编译和安装GCC7.5

    CentOS7编译和安装GCC7.5 一.    环境介绍: CentOS7 虚拟机连上了互联网(为什么要强调这点呢,因为CentOS7每次进入系统,都需要手动点击右上角的Connect,才能连上互联 ...

  3. 接口testing需要的技能

    1.什么是接口测试? 定义:测试系统组件间接口的一种测试.主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点,重点是检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关系等: 目的 ...

  4. 类linux 系统上端口被占用

    好几次遇到这问题,明明Ctrl+C退出了node,但是下次启动的时候总是会报错: listen EADDRINUSE :::80 之类的. 这时候可能是被占用,也可能是上次进程没有真的退出. ps - ...

  5. POJ3275 Ranking the Cows floyd的bitset优化

    POJ3275 Ranking the Cows #include <iostream> #include <cstdio> #include <bitset> u ...

  6. INNODB索引单列不能超767 复合不能超3072

    innodb复合索引长度为什么是3072  我们知道InnoDB一个page的默认大小是16k.由于是Btree组织,要求叶子节点上一个page至少要包含两条记录(否则就退化链表了).         ...

  7. binlog在并发状态下的记录

    前两天看binlog发现个奇怪的地方:对于position靠后的记录,timestamp却比之前的记录还要小.当时觉得大概和并发有关系 后来做了个实验 开两个session 对于session1: b ...

  8. 2.Scrapy基本命令介绍

    1.安装scrapy框架 a.安装wheel pip install wheel -i https://pypi.douban.com/simple/ b.安装twisted pip install ...

  9. poj2762 判断一个图中任意两点是否存在可达路径 也可看成DAG的最小覆盖点是否为1

      Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 179 ...

  10. POJ1436

    题目链接:https://vjudge.net/problem/POJ-1436 解题思路:基于y轴建立线段树. 如图是根据样例画出的图.下面都以题目样例为例. 但是,如果仅仅以给出的y1, y2为边 ...