android studio 30天学习笔记 -day 4-Notification

Notification一般会在手机上方的通知栏显示,举例来说,就像平时常用的line别人传送过来的讯息或是简讯,在不开启app的情况下,能从手机上方看见通知讯息的内容,在生活中可说是相当便利的功能。

今天会用Notification实作一个自我接收的通知,通知的讯息是用editText输入显示,按下button送出此通知内容

建立NotificationChannel

public void createNotificationChannel(){ //初始化NotificationManager
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //创建NotificationChannel(String id,CharSequence name,int importance)对象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ChannelID,ChannelName, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }

建立NotificationCompat.Builder

 public NotificationCompat.Builder getNotificationChannelBuilder(String msg){//创建通知相关设定
        PendingIntent pendingIntent=PendingIntent.getActivity(this,
                0,new Intent(this,MainActivity.class)
                ,PendingIntent.FLAG_CANCEL_CURRENT);
        return builder =new NotificationCompat.Builder(this,ChannelID)
                .setContentTitle("message")
                .setContentText(msg)
                .setSmallIcon(R.drawable.ic_launcher_foreground)//必填
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
    }

setContentTitle:设定通知标题
setContentText:设定通知内容
setSmallIcon:设定小图标,一定要填
setContentIntent:设定使用者点击通知时要触发的 intent 行为
setAutoCancel:让通知在使用者点击通知时自动移除

NotificationManager
是android系统负责管理及发送的类别

manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

notify
发出通知

manager.notify(1, builder.build());

MainActivity完整程序码

public class MainActivity2 extends AppCompatActivity {
    private EditText editText;
    public static final String ChannelID="myChannel";
    public static final String ChannelName ="eles";
    private NotificationManager manager;
    private NotificationCompat.Builder builder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText=findViewById(R.id.edit1);
        createNotificationChannel();//创建频道

    }
    //按钮send监听事件
    public void send(View view) { //送出通知
        sendNotificationMsg();
    }
    public void createNotificationChannel(){ //初始化NotificationManager
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //创建NotificationChannel(String id,CharSequence name,int importance)对象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(ChannelID,ChannelName, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }
    
    private void sendNotificationMsg() {
        //判断editText有没有输入值
        if (!editText.getText().toString().isEmpty()) {
            builder = getNotificationChannelBuilder(editText.getText().toString());//发出的通知内容是editText输入的字
            manager.notify(1, builder.build());
        }
        else{
            Toast.makeText(this,"不能空白",Toast.LENGTH_LONG).show();
        }
    }

    public NotificationCompat.Builder getNotificationChannelBuilder(String msg){//创建通知相关设定
        PendingIntent pendingIntent=PendingIntent.getActivity(this,
                0,new Intent(this,MainActivity.class)
                ,PendingIntent.FLAG_CANCEL_CURRENT);
        return builder =new NotificationCompat.Builder(this,ChannelID)
                .setContentTitle("message")
                .setContentText(msg)
                .setSmallIcon(R.drawable.ic_launcher_foreground)//必填
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
    }
}

结果
https://ithelp.ithome.com.tw/upload/images/20210705/20138966z7p8js9C8R.jpg
https://ithelp.ithome.com.tw/upload/images/20210705/2013896690fp4gLdT8.jpg


<<:  [Day-04] - Spring Boot Starter 环境配置马上就上手

>>:  NNI大纲描述

[DAY 02]环境建置 : 组出你的环境--前导

前言 从小到大我们都听过这样的一句话: 工欲善其事,必先利其器 ------ 书上写的 在我们开始执...

Android学习笔记01

MVVM 最近刚开始接触kotlin,而我想要用kotlin去建构一个MVVM的架构,首先要先了解甚...

D-12, Ruby 正规表达式(二) 量词 、锚 && Reverse Vowels of a String

昨天的重点复习/./就是一个最简单的正规表达式。 先认识一下match与=~。 match回传匹配的...

Day 12 均衡思考赚钱与不被花钱

依现况而言企业在强大的个资隐私规范之下,如何符合规范又不被在不知情况之下被罚又不知所云,在个资自主当...

[Day 18] - 初探永丰银行线上收款API - 丰收款 - 建立订单!

一转眼已经到第18天了,照这个速度可能没办法完成一个网站,今天要来赶进度! 首先要勘误 在nonce...