notification+Android笔记

notification通知应用UI之外的消息并显示即推送;

NotificationManager负责管理通知,例如显示取消,删除等;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
     public Button button1,btn2;
    String channelId ="001" ;//这个东西是特定的
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 =findViewById(R.id.btn1);
        btn2 =findViewById(R.id.btn2);
        button1.setOnClickListener(this);
        btn2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:

                showCommon();
                break;
            case R.id.btn2:
                break;
        }
    }
    public void showCommon() {
       // Android 8.0之后,需要手动添加NotifacationChannel实现

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("001", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(notificationChannel);

        }

        //第一步创建一个builder对线作为通知对象
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this,channelId);
      //第二步设置builder的一些参数
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置通知的小图标
        builder.setContentTitle("通知标题");//设置通知的标题
        builder.setContentText("通知内容");//设置通知的内容
        builder.setDefaults(Notification.DEFAULT_ALL);//设置通知的默认效果,例如震动,铃声等
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);//设置通知的优先级
        builder.setAutoCancel(true);//设置通知被点击后自动消失
        builder.setWhen(System.currentTimeMillis());//设置通知产生的时间
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置通知的大图标
        //第三步显示
        notificationManager.notify(001,builder.build());
    }


  
}

<?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=".MainActivity"
    android:orientation="vertical">

   <Button
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="显示一个通知"
       android:id="@+id/btn1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="显示"
        android:id="@+id/btn2"/>


</LinearLayout>

相关推荐
s_little_monster28 分钟前
【QT】QT入门
数据库·c++·经验分享·笔记·qt·学习·mfc
追光天使43 分钟前
【Mac】和【安卓手机】 通过有线方式实现投屏
android·macos·智能手机·投屏·有线
alfiy1 小时前
Elasticsearch学习笔记(六)使用集群令牌将新加点加入集群
笔记·学习·elasticsearch
爱学的小涛1 小时前
【NIO基础】基于 NIO 中的组件实现对文件的操作(文件编程),FileChannel 详解
java·开发语言·笔记·后端·nio
爱学的小涛1 小时前
【NIO基础】NIO(非阻塞 I/O)和 IO(传统 I/O)的区别,以及 NIO 的三大组件详解
java·开发语言·笔记·后端·nio
小雨cc5566ru1 小时前
uniapp+Android智慧居家养老服务平台 0fjae微信小程序
android·微信小程序·uni-app
JavaGPT2 小时前
prometheus学习笔记之PromQL
笔记·学习·prometheus
害羞的白菜2 小时前
Nginx基础详解5(nginx集群、四七层的负载均衡、Jmeter工具的使用、实验验证集群的性能与单节点的性能)
linux·运维·笔记·jmeter·nginx·centos·负载均衡
一切皆是定数2 小时前
Android车载——VehicleHal初始化(Android 11)
android·gitee
一切皆是定数2 小时前
Android车载——VehicleHal运行流程(Android 11)
android