Notification不起作用
来源:互联网 发布:知乎黑名单 编辑:程序博客网 时间:2024/06/08 11:36
今天在学习安卓Notification的时候,按照书上的代码写了一个简单的案例,但是由于没有书上的图片资源,所以注掉.setSmallIcon(R.drawable.abc)这一行代码
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Intent intent = new Intent(MainActivity.this,SecondeActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0); Notification notification =new Notification.Builder(this) //.setSmallIcon(R.drawable.abc) .setAutoCancel(true) .setTicker("有新消息") .setWhen(System.currentTimeMillis()) .setContentTitle("新消息来啦").setContentText("gggggg ").setContentIntent(pi). build();nm.notify(0x222,notification);
结果通知栏部分没有通知出现。
后来一个一个尝试后发现必须要有setSmallIcon(),哪怕仅仅只有setSmallIcon()都可以显示通知。通过查看NotificationManager.notify()代码发现在方法里面有判断过otification.getSmallIcon() == null,如果getSmallIcon为Null 会抛异常。
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) { int[] idOut = new int[1]; INotificationManager service = getService(); String pkg = mContext.getPackageName(); // Fix the notification as best we can. Notification.addFieldsFromContext(mContext, notification); if (notification.sound != null) { notification.sound = notification.sound.getCanonicalUri(); if (StrictMode.vmFileUriExposureEnabled()) { notification.sound.checkFileUriExposed("Notification.sound"); } } fixLegacySmallIcon(notification, pkg); if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { //如果没有setSmallIcon ,此处会抛异常 if (notification.getSmallIcon() == null) { throw new IllegalArgumentException("Invalid notification (no valid small icon): " + notification); } } if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); final Notification copy = Builder.maybeCloneStrippedForDelivery(notification); try { service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, copy, idOut, user.getIdentifier()); if (id != idOut[0]) { Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
0 0
- Notification 不起作用
- Notification不起作用
- Notification
- Notification
- Notification
- Notification
- notification
- Notification
- Notification
- Notification
- Notification
- Notification
- Notification
- Notification
- Notification
- notification
- Notification
- Notification
- vue中 v-if 和v-show的问题
- 【第十二周项目4 - 利用遍历思想求解图问题】
- Java注释@interface的用法
- 第十二周项目5-迷宫问题之深度优先遍历算法
- Android入门——一、Android开发基础
- Notification不起作用
- 头文件 string.h cstring string 区别
- Python连接MySql数据库
- C#中操作Excel(3)—— Excel中操作文本、图片和批注
- 最优化之路
- Thinking in Bigdata
- 1.简谈语音识别中的WFTS
- 第12周 项目-2 操作用邻接表存储的图
- Kafka vs RocketMQ ——消息及时性对比