关于通知的一些小问题

最近做下载,有一个通知显示进度的需求。这个过程中碰到一些问题,这里总结下,方便大家排查。

1.通知中下载完成后,进度条没有正确关闭掉。

我们先看官网教程中显示通知的代码:

int id = 1;
...
mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(id, mBuilder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
            }
            // When the loop is finished, updates the notification
            mBuilder.setContentText("Download complete")
            // Removes the progress bar
                    .setProgress(0,0,false);
            mNotifyManager.notify(id, mBuilder.build());
        }
    }
// Starts the thread by calling the run() method in its Runnable
).start();

进度通知比较频繁我们最好要放在子线程中操作中去,而且不能更新的太频繁,界面会有有些卡顿,甚至就是不动。我的建议是 500ms-1000ms 之间。如果频率太快,系统会直接丢弃一些更新。你们可以自己把 demo 中线程睡眠的代码去掉。观察一下现象。

如果你想要保证通知进度条正确关闭,一要保证关闭通知进度条的代码是最后执行的,尤其你是采用多线程操作的话。二是要与上一条更新有一定的时间间隔。

2.通知中的小图标没有正确显示
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
        .setSmallIcon(R.drawable.ic_notification)//small icon
        .setContentText(mContext.getString(R.string.start_download))
        .setContentTitle(mContext.getString(R.string.app_name))
        .setProgress(100, progress, false)
        .setWhen(when);

在高版本上系统上,可能会出现这种情况。小图标没有显示,而是显示一个灰色的小方块。
可以参考下图:
参考.png

这一般是你的小图标没有按照规范尺寸去设计。具体请看 规范

关于小图标我建议你使用白色的,这样如果你在高版本中让状态栏的图标变为深色的时候,系统也可以帮你把小图标变色。如果了用了其他颜色,可能就不能变色了,状态栏上的图标颜色就不统一了。

3.通知内容太长没有换行

通知内容如果太长,可以采用如下方式:

NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        style.bigText(mContext.getString(R.string.desc_guide));
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(mContext.getString(R.string.desc_insta_save_running))
                .setContentText(mContext.getString(R.string.desc_guide))
                .setContentIntent(contentIntent)
                .setStyle(style);

重点是 BigTextStyle 的使用,具体的 api 使用,大家可去看官方文档。

4.通知内容位置不固定,更新 progress 时 多条通知位置会经常变动

这是通知按照更新时间来排序的。如果你想要固定不动,可以给对应的通知传入一个固定的时间值。

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext)
        .setSmallIcon(R.drawable.ic_notification)//small icon
        .setContentText(mContext.getString(R.string.start_download))
        .setContentTitle(mContext.getString(R.string.app_name))
        .setProgress(100, progress, false)
        .setWhen(when);// 传入一个固定的时间值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值