获取系统的通知管理器实例

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  • 普通通知栏
    普通通知栏

    1
    2
    3
    4
    5
    6
    7
    8
    9
    Notification.Builder builder=new Notification.Builder(this);
    Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
    builder.setContentIntent(pendingIntent);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
    builder.setAutoCancel(true);
    builder.setContentTitle("普通通知栏");
    mNotificationManager.notify(1, builder.build());
  • 自定义布局通知栏
    自定义布局通知栏

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    notification.bigContentView=remoteViews;
    notification.contentView=remoteViews;
    Notification.Builder builder2=new Notification.Builder(this);
    Intent intent2=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
    PendingIntent pendingIntent2=PendingIntent.getActivity(this,0,intent2,0);
    builder2.setContentIntent(pendingIntent2);
    builder2.setSmallIcon(R.mipmap.ic_launcher);
    builder2.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
    builder2.setAutoCancel(true);
    builder2.setContentTitle("折叠通知");
    RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_view);
    Notification notification=builder2.build();
    notification.bigContentView=remoteViews;
    mNotificationManager.notify(1,notification);