Android Notification通知的使用

Notification : 通知

 

一 使用大纲

1  获取通知栏管理


    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);


    
2 实例化通知栏构造器


    Notification notification = new NotificationCompat.Builder()
                .setContentTitle()
                .setContentText()
                .setSmallIcon()
                 .setLargeIcon()
                .setColor()
                 .setContentIntent()
                 .setAutoCancel()
                .build();


                
3 发送通知 / 取消通知


    manager.notify(1,notification)
    manager.cancel(1)

二 代码

step1 activity_main.xml

两个按钮,对应 发送通知 和 取消通知 这个两个事件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:text="发出通知"
        android:onClick="sendMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:text="取消通知"
        android:onClick="cancelMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

step2 MyNotificationActivity.java

setContentIntent 设置通知的意图,跳转到这里的

package com.example.mynotification;

import android.os.Bundle;
import android.util.Log;

public class MyNotificationActivity extends MainActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("cs","intent 意图了");

    }
}

注意要注册一下: (否则执行不了这里的代码)

选中类名,alt + enter 快捷键,点 add activity to mainfest

 

 step3 MainActivity.java

package com.example.mynotification;

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.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    //通知的管理类,负责发通知,清除通知等操作
    NotificationManager manager;

    //通知信息类,对应通知栏的各种属性
    Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //step1  获取通知栏管理
        //NotificationManager 是一个系统 Service
        manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        //版本号 如果大于等于 8
        //NotificationManager.IMPORTANCE_HIGH :重要程度高
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("cs","测试通知",NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);

        }
        //setContentIntent 的准备工作
        //通知的意图
        //MyNotificationActivity :自己写的 通知的意图类
        Intent myIntent = new Intent(MainActivity.this , MyNotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,myIntent,0);


        //step2 实例化通知栏构造器
        //新建通知,设置通知
        //setSmallIcon 设置小图标:注意图片不能带颜色
        //setColor  设置小图标的颜色
        //setContentIntent : 设置通知的意图
        //setAutoCancel(true) :点击通知后通知就消除了
        notification = new NotificationCompat.Builder(this,"cs")
                .setContentTitle("官方通知:")
                .setContentText("通知你的女朋友到了,快过来领")
                .setSmallIcon(R.drawable.ic_cloud_queue_black_24dp)
                 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))
                .setColor(Color.parseColor("#ff0000"))
                 .setContentIntent(pendingIntent)
                 .setAutoCancel(true)
                .build();


    }

    //按钮 触发 发送通知事件
    public void sendMessage(View view) {
        manager.notify(1,notification);
    }

    //按钮 触发 取消通知事件
    public void cancelMessage(View view) {
        manager.cancel(1);
    }
}

效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值