六、Notification(通知)

一:

建一个NotificationManager

NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象, Activity-etSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。

使用Builder构造器来创建Notification对象

使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示 

二:

通知渠道: Android 8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。

通知重要程度设置, NotificationManager类中

IMPORTANCE_NONE 关闭通知

IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示

IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示

IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示

IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示 

三:常见方法说明(红色为必写项)

1. setContentTitle(String string)设置标题

2. setContentText(String string)设置文本内容

3. setSmallicon(int icon)设置小图标

4. setLargelcon(Bitmşp icon)设置通知的大图标

5. setColor(int argb)设置小图标的颜色

6. setContentIntent(Pendinglntent intent)设置点击通知后的跳转意图

7. setAutoCancel(boolean boolean)设置点击通知后自动清除通知

8. setWhen(long when)设置通知被创建的时间 

四:源码显示

ui源码

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

    <Button
        android:text="发送通知"
        android:onClick="sendNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="取消通知"
        android:onClick="celNotification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

  后台源码

package com.example.myapplication;

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 {

    private  NotificationManager manager;
    private  Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取NotifactionManager对象
        manager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        //进行版本判断
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel Channel=new NotificationChannel("leo","测试通知",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(Channel);
        }


        Intent intent = new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        notification = new NotificationCompat.Builder(this,"leo")
                .setContentTitle("官方通知")                  //设置通知的标题
                .setContentText("世界那么大,想去看看")         //设置通知的内容
                .setSmallIcon(R.drawable.ic_baseline_person_24)  //设置通知的小图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.mins)) //设置通知的大图标(图片)
                .setColor(Color.parseColor("#ffff0000"))   //设置小图标的颜色
                .setContentIntent(pendingIntent) //点击通知跳转
                .setAutoCancel(true)  //关闭通知
                .build();
    }

    public void sendNotification(View view) {
        manager.notify(1,notification);
    }

    public void celNotification(View view) {
    }


}

  

package com.example.myapplication;

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

import androidx.annotation.Nullable;

public class NotificationActivity extends Activity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Log.e("leo", "onCreate:Notification:通知跳转 ");
    }
}

  五:效果图

 

 

posted @ 2022-03-14 19:04  搬砖工具人  阅读(408)  评论(0编辑  收藏  举报