基本通知
创建builder
NotificationCompat.Builder builder = builder = new NotificationCompat.Builder(this);
至少要实现的三个方法
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content")
构造并设置PendingIntent
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(pendingIntent);
获取NotificationManager并开启通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
注意:notify的第一个参数为ID值,若有多个通知必须使用不同的ID值