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

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. jstree 反选,测试400条数据左右有点卡

    $("#reversecheckallmachines").on("change", function () { var checkedNodes = []; ...

  2. ⚠ | 不要再使用 markdown 主题了!

    前置 我在很久之前就发现了使用第三方 markdown 主题将产生一个的问题,今日在社区发现依然有人写文章来推荐这种做法.接下来我告诉你为什么最好不要这样做以及分享一些 markdown 技巧.若有不 ...

  3. 虚拟机安装配置(VMware与CentOS安装)

    VMware下载地址: https://www.vmware.com/cn/products/workstation-pro/workstation-pro-evaluation.html centO ...

  4. nodejs server启动写法

    http://www.phpstudy.net/c.php/18720.html node不利用框架怎么实现对静态HTML.css.js的服务? 初学nodeJS,在使用nodejs构建静态文件服务器 ...

  5. Linux的svn服务器搭建

    最近把Linux上的一些服务器学习了一遍 我这里更新一下笔记——SVN服务器 我从其他博主上学习了一下——转载https://www.cnblogs.com/mymelon/p/5483215.htm ...

  6. VueRouter小手册

    目录 一. 了解router 二. 工作流程 三. 简单的Demo 四. 理解template和route的组合 五. Vue-Router-GoBack记录返回 六. Router-Link 七. ...

  7. 洛谷P2754 [CTSC1999]家园

    题目链接:https://www.luogu.org/problemnew/show/P2754 知识点: 最大流 解题思路: 先用 \(DFS\) 判断是否无解. 从时刻 \(0\) 开始枚举答案, ...

  8. 第几天?(hdu2005)

    第几天那个代码模板可以保存起来. #include<stdio.h> #include<math.h> #define PI 3.1415927 using namespace ...

  9. 客服端负载均衡:Spring Cloud Ribbon

    Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具.服务间的调用,API网关的请求转发都是通过Ribbon实现的. 在微服务架构中使用客户端负载均衡需要两步: (1) ...

  10. c# 优化代码的一些规则——优先隐式类型[一]

    前言 说到底就是优先使用var,这个关键字,在c# 3.0中出现了. 首先要确认几点,一个就是var 是静态变量,而不是动态变量,也就是说使用var 你是不必去担心性能问题得, 百度百科: 1)静态存 ...