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>

相关推荐
无名-CODING22 分钟前
栈与队列学习笔记
java·笔记
NZT-4825 分钟前
C++基础笔记(二)队列deque,queue和堆priority_queue
java·c++·笔记
YJlio1 小时前
ZoomIt 学习笔记(11.7):安装与基础使用——演示/授课/录屏的神级放大镜
笔记·学习·intellij-idea
QT 小鲜肉2 小时前
【Linux命令大全】001.文件管理之chattr命令(实操篇)
linux·运维·服务器·笔记
似霰2 小时前
传统 Hal 开发笔记6----App 访问硬件服务
android·framework·hal
爱装代码的小瓶子2 小时前
【c++知识铺子】封装map和set(详细版)
android·java·c++
私人珍藏库3 小时前
AutoGLM无需豆包手机,让AI自动帮你点外卖-刷视频
android·ai·智能手机·工具·软件·辅助·autoglm
阿蒙Amon3 小时前
JavaScript学习笔记:14.类型数组
javascript·笔记·学习
XFF不秃头3 小时前
力扣刷题笔记-下一个排列
c++·笔记·算法·leetcode