Notification-状态栏上的通知

本文介绍了Android中创建和管理Notification的方法,包括使用NotificationManager、Notification和PendingIntent。讲解了如何设置PendingIntent的标志,以及如何取消Notification。还分享了Notification的高级特性,如带声音、震动、LED灯光、bigText和bigPicture,并讨论了不同优先级的效果。最后提到了自定义Notification的资源和遇到的问题链接。

当程序并不是出在运行状态的时候,可以调用Notification来显示通知。

1、创建

    Notification的创建主要涉及到三个类:NotificationManager,Notification和PendingIntent

    NotificationManager主要是对通知进行管理。

    Notification类主要用于对Notification的一些属性进行定义。获得的方式主要是通过兼容各个版本的support-v4中的NotificationCompat类来获得。

    PendingIntent类可以理解成一个延迟执行的intent。

     下面是一个实例代码:

public static final int NOTIFICATIONID = 0x101;    
private NotificationManager notificationManager;
private NotificationCompat.Builder builder;
private Notification notification;
private PendingIntent pendingIntent;
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(NotificationManiActivity.this,null);
Intent intent = new Intent(NotificationManiActivity.this,NotificationTestActivity.class);
pendingIntent = PendingIntent.getActivity(NotificationManiActivity.this,0,intent,0);
notification =builder
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)//Notification什么时候显示
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
notificationManager.notify(NOTIFICATIONID,notification);

1.1 PendingIntent

        PendingIntent的获得,可以调用自己的静态方法getActivity()、getBroadcast()、或者是getService()。参数都是一样的。第一个参数是Context,第二个参数是requestCode,第三个参数是intent。第四个参数是int类型的flag,可以分为以下四个值:FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT.分别解释为:

FLAG_ONE_SHOT:Flag indicating that this PendingIntent can be used only once表示此PendingIntent只能够使用一次。

FLAG_NO_CREATE:Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. 定义的PendingIntent如果不存在,直接返回null,不去进行创建

FLAG_CANCEL_CURRENT:Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one此标志表示,如果需要的pendingIntent已经存在,在生成新的之前应该取消当前已经存在的这个。

FLAG_UPDATE_CURRENT:Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent。此flag表示,如果PendingIntent已经存在,则不创建,但是需要将原来PendingIntent的数据用新的数据替代。

2、取消

    取消的方式有两种,分别为:

    a、调用setAutoCancel方法,点击以后,Notification会自动消失

    b、调用notificationManager.cancel方法,根据notificationId来取消Notification

实例代码如下:

    /**
     * 取消方式一
     * 调用setAutoCancel方法,点击以后,Notification会自动消失
     * @param view
     */
    public void oneCancel(){
        notification =builder
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)//点击以后,Notification会自动消失
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }
    /**
     * 取消方式二
     * 调用notificationManager.cancel方法,根据notificationId来取消Notification
     * @param view
     */
    public void twoCancel(){
        notificationManager.cancel(NOTIFICATIONID);//根据notificationId来取消Notification
    }

3、技巧

    3.1 Notificationt带声音

    /**
     * notificationt带声音
     * @param view
     */
    public void sound(){
        notification =builder
                .setContentTitle("notificationt带声音")
                .setContentText("notificationt带声音")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luma.ogg")))//设置Notification创建的时候的提示音
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.2 Notificationt带震动

    /**
     * notificationt带震动
     * @param view
     */
    public void vibrate(View view){
        notification =builder
                .setContentTitle("notificationt带震动")
                .setContentText("notificationt带震动")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                //设置震动的频率,小标为0的静止的时长,下标为1的表示振动的时长,小标为2的静止的时长,以此类推
                .setVibrate(new long[]{0,1000,1000,1000})
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.3 Notificationt带LED灯光

    /**
     * Notificationt带LED灯光
     * @param view
     */
    public void led(View view){
        notification =builder
                .setContentTitle("Notificationt带LED灯光")
                .setContentText("Notificationt带LED灯光")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                //LED灯光颜色,LED灯光亮起时长,LED灯光暗去时长
                .setLights(Color.GREEN,1000,1000)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.4 Notificationt带bigText

    /**
     * Notificationt带bigText
     * @param view
     */
    public void bigText(View view){
        notification =builder
                .setContentTitle("Notificationt带bigText")
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Notificationt带bigTextNotificationt带bigTextNotificationt带" +
                        "bigTextNotificationt带bigTextNotificationt带bigTextNotificationt带"+
                        "bigTextNotificationt带bigTextNotificationt带bigTextNotificationt带bigT" +
                        "extNotificationt带bigTextNotificationt带bigTextNotificationt带bigText" +
                        "Notificationt带bigTextNotificationt带bigText"))
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    效果图为:

bd2255852098e8406150f962bb371fb616b.jpg

注意:这个是模拟器的效果,对于部分真机,则直接不显示文字内容

    3.5 Notificationt带bigPicture

        notification =builder
                .setContentTitle("Notificationt带bigPicture")
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round)))
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);

    注意:这个对模拟器有效果,对于部分真机,则直接不显示文字内容

3.6 Notificationt带priority

    /**
     * Notificationt带priority
     * @param view
     */
    public void priority(View view){
        notification =builder
                .setContentTitle("Notificationt带priority")
                .setContentText("Notificationt带priority")
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    优先级的参数从低到高分别为:PRIORITY_DEFAULT表示默认的重要程度,和不设置效果是一样的。PRIORITY_MIN表示最低的重要程度,系统只会在特定的场景中才会显示这条通知,比如用户下拉状态栏的时候;PRIORITY_LOW表示较低的重要程度,系统可能会将这类通知缩小,或者是改变其显示的顺序(有一部分手机会修改显示顺序),并将其排在更重要的通知之后;PRIORITY_HIGH表示较重的重要程度,系统可能会将这类通知放大,或者是改变其显示顺序,排在比较靠前的位置;PRIORITY_MAX表示最重要的通知,有一些手机上面会直接显示成一个弹窗的形式,有一些还是改变显示顺序。

4、自定义部分

      参考地址:https://developer.android.com/training/notify-user/custom-notification#java

      总结的比较好的地址:https://blog.youkuaiyun.com/u011200604/article/details/52470770

      Notification中会遇到的一些坑:https://www.jianshu.com/p/4ee590340803

     guide地址:https://developer.android.com/guide/topics/ui/notifiers/notifications#CreateNotification

     design中的内容:https://link.jianshu.com/?t=https://material.io/guidelines/patterns/notifications.html#notifications-templates

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值