一、通知(Notification)
1.获得NotificationManager
2.获取Notification
3.设置Notification属性;
4.设置PendingIntent:点击通知跳转;
5.显示通知manager.notify(int id, Notification notification);
例:
private void showNotify() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(MainActivity.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("my'notify");
builder.setContentTitle("this is title");
builder.setContentText("this is content");
Intent intent = new Intent(MainActivity.this, NotifiActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this ,0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_ALL;
manager.notify(1, notification);
}
△点击Notification之后消失:
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1); //参数为notityId;