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>

相关推荐
wdfk_prog43 分钟前
[Linux]学习笔记系列 --[drivers][base]map
linux·笔记·学习
浅念-1 小时前
链表经典面试题目
c语言·数据结构·经验分享·笔记·学习·算法
2501_944424121 小时前
Flutter for OpenHarmony游戏集合App实战之俄罗斯方块七种形状
android·开发语言·flutter·游戏·harmonyos
啥都会点的大秀2 小时前
声学仿真学习笔记
笔记·学习
不会代码的小猴2 小时前
Linux环境编程第三天笔记
linux·笔记
狐572 小时前
2026-01-18-LeetCode刷题笔记-1895-最大的幻方
笔记·算法·leetcode
不会Android的潘潘3 小时前
受限系统环境下的 WebView 能力演进:车载平台 Web 渲染异常的根因分析与优化实践
android·java·前端·aosp
建军啊3 小时前
java web常见lou洞
android·java·前端
豆奶dudu3 小时前
安卓应用签名生成+微信开放平台安卓应用签名
android·微信开放平台
AI视觉网奇3 小时前
Epic linux 打包。
笔记·学习·ue5