创建一个NotificationManager

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

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

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

NotificationChannel

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

通知重要成都设置,NotificationManager类中

IMPORTANCE_NONE 关闭通知
IMPORTANCE_MIN 关闭通知,不会弹出,没有提示音,状态栏中无显示
IMPORTANCE_LOW 开启通知,不会弹出,没有提示音,状态栏中显示
IMPORTANCE_DEFAULT 开启通知,不会弹出,有提示音,状态栏中显示
IMPORTANCE_HIGH 开启通知,会弹出,有提示音,状态栏中显示

常见方法说明

.setContentTitle(“官方通知”):设置标题
.setContentText(“出去整根烟?”):设置文本内容
.setSmallIcon(R.drawable.ic_baseline_person_24):设置小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ceshi)):设置通知的大图标
.setColor(Color.parseColor(“#ff00ff”)):设置小图标颜色
.setContentIntent(activity):设置点击通知后的跳转意图
.setAutoCancel(true):设置点击通知后自动清除通知
.setWhen():设置通知被创建的时间

注:安卓从5.0系统开始,对于通知栏图标的设计进行了修改。现在谷歌要求,所有应用程序的通知栏图标,应该只使用alpha图层来绘制,而不应该包括RGB图层

前端示例

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

  1. <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="按钮1"<br /> android:onClick="send"<br /> android:id="@+id/button1"<br /> /><br /> <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:onClick="load"<br /> android:text="按钮2"<br /> android:id="@+id/button2"<br /> /><br /></LinearLayout>

后端示例

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.Bitmap;
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 notificationManager;

  1. private Notification notification;<br /> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout._activity_main_);<br /> notificationManager = (NotificationManager)getSystemService(_NOTIFICATION_SERVICE_);<br /> //测试系统版本<br /> if(Build.VERSION._SDK_INT_>=Build.VERSION_CODES._O_){<br /> NotificationChannel channel = new NotificationChannel("Beleth", "测试通知", NotificationManager._IMPORTANCE_HIGH_);<br /> notificationManager.createNotificationChannel(channel);<br /> }<br /> //触发对象设置<br /> Intent intent = new Intent(this,NotificationActivity.class);<br /> PendingIntent activity = PendingIntent._getActivity_(this, 0, intent, 0);<br /> //主体程序<br /> notification = new NotificationCompat.Builder(this,"Beleth")<br /> .setContentTitle("官方通知")<br /> .setContentText("出去整根烟?")<br /> .setSmallIcon(R.drawable._ic_baseline_person_24_)<br /> .setLargeIcon(BitmapFactory._decodeResource_(getResources(),R.drawable._ceshi_))<br /> .setColor(Color._parseColor_("#ff00ff"))<br /> .setContentIntent(activity)<br /> .setAutoCancel(true)<br /> .build();
  2. }
  3. public void send(View view) {<br /> notificationManager.notify(1,notification);<br /> }
  4. public void load(View view) {<br /> notificationManager.cancel(1);<br /> }<br />}