隐藏

Java.Lang.IllegalArgumentException: 'Invalid notification (no valid small icon): Notification(pri=0

发布:2021/10/13 15:24:59作者:管理员 来源:本站 浏览次数:1628

由于系统样式自带的是必须设置三个属性

setContentText()
setContentTitle()
setSmallIcon() 
不然会报错:比如

java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.grandmagic.readingmate/0x7f04008c headsUpContentView=null bigContentView=null vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE) at android.app.NotificationManager.notify(NotificationManager.java:225) at android.app.NotificationManager.notify(NotificationManager.java:197)

想要使用自定义的Notification的时候

当然是要使用

RemoteViews这个咯,需要注意的是,它的构造方法

new RemoteViews(mAppContext.getPackageName(), R.layout.notification_view);


需要传入包名和布局,布局中能用的控件是有限的,

搜索了一下说是只能使用

AnalogClock,Button,Chronometer,ImageButton,mageView,ProgressBar,TextView这7种,


布局中的宽,必须为0,wrap_content,或match_parent

因为从别的地方拷贝的布局。里面用的居然是fill_parent。果断抛出异常,源码中是这样的

 if (layoutWidth != 0 && layoutWidth != ViewGroup.LayoutParams.MATCH_PARENT

           && layoutWidth != ViewGroup.LayoutParams.WRAP_CONTENT) {

       throw new IllegalArgumentException("Only supports 0, WRAP_CONTENT and MATCH_PARENT");

   }


,然后之所以使用自定义布局,就是想通知栏显示好友头像,而不是固定的资源文件而已,所以加载图片的时候可能需注意。一般是先下载图片在设置Notification,比如我用Glide下载,需要注意Glide要求into()方法必须在主线程执行,所以用了Rxjava切换线程,毕竟不是Activity的context不能runOnUithread();

  Observable

           .just(Environment.BASEULR_PRODUCTION+mUserInfo.getAvatar_url().getMid())

           .flatMap(new Func1<String, Observable<BitmapTypeRequest<String>>>() {

               @Override

               public Observable<BitmapTypeRequest<String>> call(String mS) {

                   BitmapTypeRequest<String> mStringBitmapTypeRequest = Glide.with(mAppContext).load(mS).asBitmap();

                   return Observable.just(mStringBitmapTypeRequest);

               }

           })

           .observeOn(AndroidSchedulers.mainThread())

           .subscribe(new Subscriber<BitmapTypeRequest<String>>() {

               @Override

               public void onCompleted() {


               }


               @Override

               public void onError(Throwable e) {

                showNotification(mUserInfo,mNotifytxt,mMessage,null);

               }


               @Override

               public void onNext(BitmapTypeRequest<String> mStringBitmapTypeRequest) {

                   mStringBitmapTypeRequest.into(new SimpleTarget<Bitmap>() {

                       @Override

                       public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                           showNotification(mUserInfo,mNotifytxt,mMessage,resource);

                       }

                   });

               }

           });


其实最后发现,自定义的RemoteViews会遇到适配的麻烦。不同的ROM拉下来的通知栏是不一样的。比如我android 4.4的模拟器是黑色的,而魅族flyme6.0的系统通知拉下来的背景是灰色的