Android 3种Notification使用方法

Notification可以让我们在获得消息的时候在状态栏,以及锁屏界面显示对应的信息,接下来介绍3种Notification,分别是普通Notification,折叠式Notification,悬挂式Notification。

Notification 的创建主要涉及到 Notification.Builder 、 Notification 、 NotificationManager 。

  1. Notification.Builer : 使用建造者模式构建 Notification 对象。由于 Notification.Builder 仅支持 Android 4.1及之后的版本,为了解决兼容性问题, Google 在 Android Support v4 中加入了 NotificationCompat.Builder 类。对于某些在 Android 4.1 之后才特性,即使 NotificationCompat.Builder 支持该方法,在之前的版本中也不能运行。点我 查看更多关于 Notification 兼容性问题处理。
  2. Notification : 通知对应类,保存通知相关的数据。NotificationManager 向系统发送通知时会用到。
  3. NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法可以向系统发送通知。

首先我们先创建一个NotificationManager :

        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

1.​​​​​普通Notification 

Notification.Builder builder=new Notification.Builder(MainActivity.this);
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);

                builder.setContentIntent(pendingIntent);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
                builder.setAutoCancel(true);
                builder.setContentTitle("这是普通Notification");
                builder.setContentText("这是通知内容");
                builder.setWhen(System.currentTimeMillis());

                Notification notification=builder.build();
                manager.notify(1,notification);

效果:

2.折叠Notification

Notification.Builder builder=new Notification.Builder(MainActivity.this);
                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);

                builder.setContentIntent(pendingIntent);
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher));
                builder.setAutoCancel(true);
                builder.setContentTitle("这是折叠Notification");
                builder.setContentText("这是通知内容");
                builder.setWhen(System.currentTimeMillis());
                /**这是与普通Notification不同的地方
                 * 用了RemoteViews*/
                RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.view_hold);
                Notification notification=builder.build();

                /**展开的视图*/
                notification.bigContentView=remoteViews;
                manager.notify(2,notification);

效果:

3.悬挂Notification

Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/mountain_hua"));
                PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,0,intent,0);
                /**悬挂的设置*/
                Intent hangIntent=new Intent();
                hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent hangPendingIntent=PendingIntent.getActivity(MainActivity.this,0,hangIntent,PendingIntent.FLAG_CANCEL_CURRENT);
                Notification.Builder builder = new Notification.Builder(MainActivity.this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        //点击通知后自动清除
                        .setAutoCancel(true)
                        .setContentTitle("这是悬挂Notification")
                        .setContentText("这是通知内容")
                        .setWhen(System.currentTimeMillis())
                        //如果加上下面这句,通知就不会自动消失,需要用户上滑才能消失,来电显示即是如此
                        //.setFullScreenIntent(hangPendingIntent,true)
                        .setDefaults(~0)//这两句是使悬挂通知自动消失
                        .setPriority(Notification.PRIORITY_HIGH)//这两句是使悬挂通知自动消失
                        .setContentIntent(pendingIntent);

                //发送通知
                manager.notify(3, builder.build());

效果:

 

参考资料:Android Notification 详解

                  悬挂通知不自动消失问题

                  Android Notification 的四种使用方式

猜你喜欢

转载自blog.csdn.net/mountain_hua/article/details/81163728