相关文章 通知的各种类型
Android6.0后setLatestEventInfo和new Notification()方法弃用,首先看一下setLatestEventInfo方法的使用:
Notification notification = new Notification(drawable,tickerText,System.currentTimeMillis()); //此处定义了一个Notification
其中第一个参数代表图标第二个参数代表提示的内容,第三个参数是指要显示的时间,一般是当即显示,故填入系统当前时间。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityMain.class), 0);//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity.
notification.setLatestEventInfo(this, title, content, contentIntent);//在此处设置在notity列表里的该notifycation得显示情况。
替代方法:
notification = new Notification.Builder(context) .setChannelId(String.valueOf(Notification_ID))//该句适配android 8.0 版本 .setSmallIcon(notifyIcon) .setContentTitle(notifyTitle) .setTicker(text) .setContentText(text) .setWhen(when) .setContentIntent(pi).setNumber(0) .getNotification(); notification.flags |= Notification.FLAG_AUTO_CANCEL;