如何使用Notification的相关Style实现不同类型的Notification

本文详细介绍了如何在Android中使用Notification的各种Style,包括BigText、BigPicture、Inbox、Media、Message、带有回复功能的Notification及HeadUp等。通过实例代码展示了各类型Notification的创建和效果,同时提到了版本要求和自定义布局的实现。
摘要由CSDN通过智能技术生成

Notification基本上是每个商用APP都会使用的一个用户交互控件(其实我也不是很清楚Notification到底是属于控件还是UI还是都包含了),好了,这不影响下面继续介绍如何正确的使用Notification!!!

好了,废话不多少,从这篇博客中您可以学到如何使用Notification,使用Notification的BigText样式、bigPicture样式、Inbox样式、如何使用mediaNotification、MessageNotification、带有回复功能的notification以及HeadUpNotification!!!
其中有些Notification的功能对于系统的版本存在要求,比带有回复功能的Notification要求SDK版本为24等!!!
先看一下Notification的知识点:
Notification的相关Flag:

Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清除该通知
Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务

Notification的优先级:

Notification.PRIORITY_DEFAULT
Notification.PRIORITY_HIGH
Notification.PRIORITY_LOW
Notification.PRIORITY_MAX
Notification.PRIORITY_MIN

分别代表数字:-2 ~ +2,默认Notification的优先级为0;

现在开始一个一个介绍:
1、首先看一下最普通的Notification,只有三个元素:
1、SmallIcon —通知栏的Icon
2、ContentTitle — 通知栏的标题
3、ContentText — 通知栏的详细内容

效果图如下所示:
这里写图片描述
具体的设置代码如下:

    public void showNormalNotification(View view) {
        Log.i("zyq", "showNormalNotification");
        Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
        mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round));
        mBuilder.setContentTitle("this is Notification Title");
        mBuilder.setContentText("this is Notification Text");
        Intent mIntent = new Intent();
        ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity");
        mIntent.setComponent(name);
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0);
        mBuilder.setContentIntent(mPendingIntent);
        Notification n = mBuilder.build();
        mNotificationManager.notify(99, n);
    }

在这里没有对Notification的Flag进行设置,需要的情况下可以设置Notification的Flag已达到我们想要的体验效果!

是不是很简单,如果你需要的只是一个提醒通知栏的话,可能连点击跳转的PendingIntent都不需要设置!

2、在上一Notification的基础上通过添加代码设置Style,完成一个BitTextNotification,这种类型的Notification在显示较长的文字提醒信息时可以用到,老规矩,还是看一下效果图吧!
这里写图片描述

效果感觉还不错,而且系统很贴心,设置了Style的通知栏,可以进行折叠,就跟gif显示的一样!!

实现代码:

public void showBigTextNotification(View view) {
        Log.i("zyq", "showBigTextNotification");
        Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
        mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round));
        mBuilder.setContentTitle("this is Notification Title");
        mBuilder.setContentText("this is Notification Text");
        Intent mIntent = new Intent();
        ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity");
        mIntent.setComponent(name);
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0);
        mBuilder.setContentIntent(mPendingIntent);
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        Notification.BigTextStyle mBigTextStyle = new Notification.BigTextStyle();
        mBigTextStyle.setBigContentTitle("This is BigContentTitle");
        mBigTextStyle.setSummaryText("This is BigSummaryText");
        mBigTextStyle.bigText("This is BigText \n 如果。所有的伤痕都能够痊愈。 如果。所有的真心都能够换来真意。 如果。所有的相信都能够坚持。如果。所有的情感都能够完美。如果。依然能相遇在某座城。单纯的微笑。微微的幸福。肆意的拥抱。 该多好。可是真的只是如果。");
        mBuilder.setStyle(mBigTextStyle);
        Notification n = mBuilder.build();
        mNotificationManager.notify(98, n);
    }

在这里没有对Notification的Flag进行设置,需要的情况下可以设置Notification的Flag已达到我们想要的体验效果!

3、完成BigPictureNotification

通过名字就可以看出来这个样式的Notification跟BigTextNotification有很大的相似之处,在实现代码上没有多大的区别,只不过是传入的Style不同,先看一下效果图吧:
这里写图片描述

实现代码:

public void showBigPictureNotification(View view) {
        Log.i("zyq", "showBigPictureNotification");
        Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
        mBuilder.setSmallIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher_round));
        mBuilder.setContentTitle("this is Notification Title");
        mBuilder.setContentText("this is Notification Text");
        Intent mIntent = new Intent();
        ComponentName name = new ComponentName("com.example.notificationdemo", "com.example.notificationdemo.MainActivity");
        mIntent.setComponent(name);
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, mIntent, 0);
        mBuilder
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值