创建Notification Channel
NotificationChannel channel = new NotificationChannel(channelId,channelName,importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
//channelId指的是Channel的id,我们可以自己定义,类型为字符串
//channelName指的是Channel的Name,我们也可以自己定义,类型为字符串
/*
importance指的是通知的重要程度即通知的优先级,有下面几种:
IMPORTANCE_NONE:不提示,不展示
IMPORTANCE_MIN:不提示,在通知下拉栏会展示,但是是收起的
IMPORTANCE_LOW:会在状态栏中显示,但不会弹窗,通知下拉栏会展示
IMPORTANCE_DEFAULT:会在状态栏中显示,允许有声音提示,但不会弹窗,通知下拉栏会展示
IMPORTANCE_HIGH:会弹窗提示,允许有提示音
IMPORTANCE_MAX:会弹窗提示,允许有提示音,可以使用全屏
*/
通过上面代码段的操作,就在系统中为本应用创建了一个消息Channel
弹出通知
/*
首先获得通知管理器的对象
new NotificationCompat.Builder(MainActivity.this,"chat1")
MainActivity.this是上下文对象
"chat1"是指定的消息Channel,我们在创建Channel模块定义的
setContentTitle("Mr Lei") 用于设置通知内容的标题
setContentText("Are you ok?") 用于设置通知内容
setWhen(Sytemt.currentTimeMillis()) 通知的发布事件,这儿设置的是系统收到该通知的事件
setSmallIcon(R.mipmap.people) 设置小图标 **此项不需写**
setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people)) 设置大图标
setAutoCancel(true).build(); 是否点击小时
mNotificationManager.notify(1, mNotification); 发布通知
1 整形数据,是用于唯一标识通知的,我们自行设置
mNotificaton Notification对象
*/
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification = new NotificationCompat.Builder(MainActivity.this, "chat1")
.setContentTitle("Mr Lei")
.setContentText("Are you ok?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.people)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people))
.setAutoCancel(true).build();
mNotificationManager.notify(1, mNotification);
简易的通知Demo
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
NotificationManager mNotificationManager;
Notification mNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initChannel();
initWidget();
}
private void initChannel() {
String channelId1 = "chat1";
String channelName1 = "IMPORTANCE_DEFAULT";
int importance1 = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId1, channelName1, importance1);
}
private void initWidget() {
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification = new NotificationCompat.Builder(MainActivity.this, "chat1")
.setContentTitle("Mr Lei")
.setContentText("Are you ok?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.people)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.people))
.setAutoCancel(true).build();
mNotificationManager.notify(1, mNotification);
break;
}
}
}