创建一个NotificationManager
Activity.getSystemService(String)
方法获取Context.NOtifICAtiON_SERVICE来返回到NotificationManagermanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//创建一个NotificationManager
使用Builder构造器来创建Notification对象
//Notification notification =new NotificationCompat.Builder(context,channelId).build();
notification = new NotificationCompat.Builder(this, "zgs")
.setContentTitle("zgs设置的通知")//设置标题
.setContentText("点击通知进入登录界面")//设置文本内容
.setSmallIcon(R.drawable.ic_baseline_favorite_border_24)//设置小图标
.setColor(Color.parseColor("#ff0222"))//设置小图标颜色
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bairen))//设置大图标
.setContentIntent(pendingIntent)//跳转事件
.setAutoCancel(true)//点击后消失
.build();
NotificationChanel 通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//版本判断:8.0版本以上才用得到
channel = new NotificationChannel
("zgs", "NotificationText", NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(channel);
}