Android3种Notification实现方法详解

前言

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。下面会分别实现普通的通知,带自定义视图的通知,还有悬挂似的通知。如果有大佬发现哪里有瑕疵请评论区留言指点,感激不尽!文末准备了一份完整系统的进阶提升的技术大纲和学习资料,希望对于有一定工作经验但是技术还需要提升的朋友提供一个方向参考,以及免去不必要的网上到处搜资料时间精力。

3种方式开始前都要先执行下面这行代码:

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

1.普通通知:

效果图:

gif1.gif


创建Builder对象,利用PendingIntent来跳转,然后给Builder添加各种属性。

完整的代码如下:

  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());

2.带视图的通知

效果图:

gif2.gif

自定义视图有两种状态,一种是上面的效果图,即展开状态的视图;另一种是普通状态下的视图,需要我们手动的拉一下,折叠部分才会出来。我们需要用到RemoteViews哎创建视图。

视图的布局我就不贴出来了,就是个普通的水平布局.

指定展开状态的视图:

 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);

3.悬挂式的通知

效果图:

gif3.gif

悬挂式是Android5.0以后的新特性,前两种需要手动拉通知栏,而这种方式不需要,直接就可以显示在屏幕上方.

完整代码如下:

 Notification.Builder builder3=new Notification.Builder(this);
          Intent intent3=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.jianshu.com/p/82e249713f1b"));
          PendingIntent pendingIntent3=PendingIntent.getActivity(this,0,intent3,0);
          builder3.setContentIntent(pendingIntent3);
          builder3.setSmallIcon(R.mipmap.ic_launcher);
          builder3.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
          builder3.setAutoCancel(true);
          builder3.setContentTitle("悬挂通知");

          Intent XuanIntent=new Intent();
          XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          XuanIntent.setClass(this,MainActivity.class);

          PendingIntent xuanpengdIntent=PendingIntent.getActivity(this,0,XuanIntent,PendingIntent.FLAG_CANCEL_CURRENT);
          builder3.setFullScreenIntent(xuanpengdIntent,true);
          mNotificationManager.notify(2,builder3.build());

附加:Android5.0以后可以设置通知的等级

  • VISIBILITY_PUBLIC: 任何情况的显示
  •  
  • VISIBILITY_PRIVATE: 只有在没有锁屏时显示
  •  
  • VISIBILITY_SECRET: 在安全锁下或者没锁屏下显示
  •  
  • Android5.0以后可以通过builder.setVisibility(Notification.VISIBILITY_PUBLIC);设置.

最后

针对于有几年工作经验的Android开发者朋友,可能不知道如何进阶成为高级工程师,在技术上遇到了瓶颈不知道发展方向。我总结出了很多一线互联网公司Android程序员面试涉及到的绝大部分面试题及答案,做成了文档和架构视频资料,免费分享给大家

(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助你提供一个学习的方向,最快的提高和完善自己的技术水平,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

资料领取方式:

加Android技术交流群;964557053,找群管理免费领取。备注一下csdn看到的来领取资料就可以了!

猜你喜欢

转载自blog.csdn.net/feiyu1947/article/details/87907978
今日推荐