创建一个NotificationManager
NotificationManager类是一个通知管理类,这个对象是由系统维护的服务,是以单例模式的方式获取的,所以一般并不直接实例化这个对象。在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可。
使用Builder构造器来创建Notification对象
使用NotificationCompat类的Buider构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果没有设置,则通知无法在Android8.0的机器上显示
NotificationChannel
通知渠道。Android8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可之定义的渠道。
通知重要程度设置,NotificationManager类中
- IMPORTANCE_NONE 关闭通知
- IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
- IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
- IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
- IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
常见方法说明
- setContentTitle(String string) 设置标题
- setContentText(String string) 设置文本内容
- setSmallicon(int icon) 设置小图标 这个小图标是没有颜色的
- setColor(int argb) 设置小图标的颜色
- setLargeIcon(Bitmap icon) 设置通知的大图标
- setContentintent(PendingIntent intent) 设置点击通知后的跳转意图
- setAutoCancel(boolean boolean) 设置点击通知后自动清除通知
- setWhen(long when) 设置通知被创建的时间
package com.example.mybutton;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
private Notification notification;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//notification通知管理类
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//对手机系统版本进行一个判断,只有Android8.0以上才创建
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//这个时候我就去创建一个NotificationChannel
//NotificationChannel里面有三个参数,第一id要和Notification的一致,第二个就是他的名字,可以随意设置,第三个是重要程度,需要通过NotificationManager类来设置
NotificationChannel channel = new NotificationChannel("wang", "测试通知", NotificationManager.IMPORTANCE_HIGH);
//我们创建完使用的时候,需要用Notification通知管理类把它放进去
manager.createNotificationChannel(channel);
}
//配合setContentIntent点击通知跳转
Intent intent = new Intent(this, HomeActivity.class);
//因为通知需要使用PendingIntent来跳转
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//这里的channelId是Android8.0引进的通知渠道 需要通过NotificationChannel进行设置 所以下面需要创建一个NotificationChannel对象
notification = new NotificationCompat.Builder(this,"wang")
.setContentTitle("官方通知")
.setContentText("世界那么大,想去走走嘛")
//通知图标应该使用纯色的图片,因为Android从5.0系统开始,对于图标设计进行了修改,所有应用程序
//的通知栏图标,应该使用alpha图层,其实就是没有颜色的图片
.setSmallIcon(R.drawable.ic_android_black_24dp)
//给小图标设置颜色
.setColor(Color.parseColor("#ff0000"))
//设置大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_foreground))
//点击通知时跳转
.setContentIntent(pendingIntent)
//设置点击通知后清除通知
.setAutoCancel(true)
//设置通知被创建的时间 不设置的话就显示的是当前系统时间
// .setWhen()
.build();
}
public void setnNotification(View view) {
//发送通知
manager.notify(1,notification);
}
public void cacelNotification(View view) {
//取消通知
manager.cancel(1);
}
}
这是布局:
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="发送消息"
android:onClick="setnNotification"
/>
<Button
android:layout_width="100dp"
android:layout_height="50dp"
android:text="取消通知"
android:onClick="cacelNotification"
/>
</LinearLayout>
到这里你的第一个Notification小Demo算是完成了