道客优

1234
Android通知(Notification)的使用方法
2019-03-27 刀刻油 阅读:1462

在使用手机时,我们常常会碰到各种通知,例如微信,头条,UC等,天天不厌其烦的给你各种推送,当然了我们今天不讲推送,我们讲讲通知栏的构建和使用,以及自定义通知栏的布局和使用方法

构建一个通知栏一般分为这几个步骤:

1.创建通知栏管理工具
2.构建通知栏构造器
3.给构造器设置参数
4.发送请求

具体代码如下:

       /**
         *  创建通知栏管理工具
         */

        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);

        /**
         *  实例化通知栏构造器
         */

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

        /**
         *  设置Builder
         */
        //设置标题
        mBuilder.setContentTitle("我是标题")
                //设置内容
                .setContentText("我是内容")
                //设置大图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                //设置小图标
                .setSmallIcon(R.mipmap.ic_launcher_round)
                //设置通知时间
                .setWhen(System.currentTimeMillis())
                //首次进入时显示效果
                .setTicker("我是测试内容")
                //设置通知方式,声音,震动,呼吸灯等效果,这里通知方式为声音
                .setDefaults(Notification.DEFAULT_SOUND);
        //发送通知请求
        notificationManager.notify(10, mBuilder.build());

因为我在代码中注释的比较清楚,这里就不一一赘述了

20180408143640766.png20180408143701310.png

显然直接使用原生的通知栏会在不通顺手机上显示不同效果,无法形成统一性,也不是特别美观,所以我们需要自定义通知栏

自定义通知栏和使用原生的通知栏区别不大,最主要就是增加了自定义的布局,使用RemoteViews承接,并放入构造器中显示,具体代码如下

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        mBuilder.setSmallIcon(R.mipmap.timg);
        mBuilder.setContent(remoteViews);
        if (progress == 1) {
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }
        remoteViews.setImageViewResource(R.id.image, R.mipmap.timg);
        remoteViews.setTextViewText(R.id.title, "我是标题");
        remoteViews.setTextViewText(R.id.content, "我是内容");
        remoteViews.setProgressBar(R.id.pBar, 10, progress, false);
        remoteViews.setTextViewText(R.id.proNum, progress + "/10");
        notificationManager.notify(10, mBuilder.build());

自定义标题栏
20180408144447526.png

点击跳转到其他页面:

   Intent intent = new Intent(this, SecondeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        mBuilder.setContentIntent(pendingIntent);


到此,具体工作就已经完成的差不多了,你会用了吗,当然还有很多知识点,想学更多的还有可以去看具体源码

遇到的问题:
(1)
在Android O及以上版本报Developer warning for package “”
Failed to post notification on channel “null”
see log for more details 错误,错误原因是因为在8.0以上需要增加渠道名称和渠道ID,具体代码如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(id, name, NotificationManager
                    .IMPORTANCE_DEFAULT);
            mBuilder.setChannelId(id);
            notificationManager.createNotificationChannel(channel);

            mBuilder.setSmallIcon(R.mipmap.timg);
            mBuilder.setContent(remoteViews);
            if (progress == 1) {
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
            }
            remoteViews.setImageViewResource(R.id.image, R.mipmap.timg);
            remoteViews.setTextViewText(R.id.title, "我是标题");
            remoteViews.setTextViewText(R.id.content, "我是内容");
            remoteViews.setProgressBar(R.id.pBar, 10, progress, false);
            remoteViews.setTextViewText(R.id.proNum, progress + "/10");
            notificationManager.notify(10, mBuilder.build());
        }
  

(2)mBuilder.setSmallIcon()是必须要加上的,这个是显示在顶部状态栏中的小图标,如果未加这个图标程序将会闪退,并报以下错误
java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=cm.cui.testnotification/0x7f09001d vibrate=null sound=default defaults=0x1 flags=0x0 color=0x00000000 vis=PRIVATE)

源码

推荐阅读: