Android平台提供了默认的通知栏实现,如下的代码就能实现:

NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(context);builder.setContentTitle(getNotificationTitle())       .setContentText(getNotificationContent())       .setSmallIcon(iconDrawableId)       .setTicker(getNotificationContent())       .setAutoCancel(true)       .setContentIntent(pendingIntent);manager.notify(notifyTitleId, builder.build());
这种方式虽然简单,但图标不能为网络图片,因此我们需要使用自定义的方式。

刚开始想先显示通知栏,之后异步下载并图片并更新图标,但却一直不能显示网络图片,如下:

ImageRequest request = new ImageRequest(getNotificationStatusIcon(),        new Response.Listener(){            @Override            public void onResponse(Bitmap response) {                remoteView.setImageViewBitmap(iconId, response);            }        }, 0, 0, Bitmap.Config.RGB_565,        new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError error) {             }        });
但图片获取成功了,显示无法成功。后来改变了顺序,先将图片下载了,之后再显示通知栏,居然显示正常了。

ImageRequest request = new ImageRequest(getNotificationStatusIcon(),                           new Response.Listener(){                                @Override                                public void onResponse(Bitmap response) {                                    pushCustomNotification(context, response);                                }                            }, 0, 0, Bitmap.Config.RGB_565,                            new Response.ErrorListener() {                                @Override                                public void onErrorResponse(VolleyError error) {                                    pushDefaultNotification(context);                                }                            });......private void pushCustomNotification(Context context, Bitmap icon){        Intent intent;        try {            intent = Intent.parseUri(getIntent(), 0);            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);            final int notifyTitleId = ResourceUtil.getStringId(context,"push_mail_notification_title");            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);            final int iconDrawableId = ResourceUtil.getDrawableId(context, "mail_push_icon");            final int iconId = ResourceUtil.getId(context, "push_notification_icon");            final RemoteViews view = new RemoteViews(context.getPackageName(), ResourceUtil.getLayoutId(context, "layout_custom_notification"));            view.setTextViewText(ResourceUtil.getId(context, "push_notification_title"), getNotificationTitle());            view.setTextViewText(ResourceUtil.getId(context, "push_notification_content"), getNotificationContent());            view.setImageViewBitmap(iconId, icon);            builder.setCustomContentView(view)                    .setSmallIcon(iconDrawableId)                    .setTicker(getNotificationContent())                    .setAutoCancel(true)                    .setContentIntent(pendingIntent);            manager.notify(notifyTitleId+1, builder.build());        } catch (URISyntaxException e) {            e.printStackTrace();        }    }






更多相关文章

  1. 兼容android Q存图片到相册
  2. android layer-list
  3. 可循环显示图像的Android(安卓)Gallery组件
  4. Android手机开发:图片的放大和缩小显示ImageView
  5. android 打开app先显示欢迎界面后自动跳到主界面
  6. Android(安卓)GridView 例子
  7. 可循环显示图像的Android(安卓)Gallery组件
  8. android actionBar右上角 menu 显示三个点
  9. 可循环显示图像的Android(安卓)Gallery组件

随机推荐

  1. 面向对象编程基础
  2. 接口和抽象类
  3. 面向对象封装,继承,加载器
  4. CentOS 6.5 用光盘镜像制作本地yum源
  5. php常量知识点的总结及过滤器的使用(1118
  6. RocketMQ-Spring 毕业两周年,为什么能成为
  7. 春节期间,课程6折特惠
  8. 刷票小程序案例原理剖析(python和java)
  9. 大公司面试必备这些知识点
  10. python单下划线开头和双下划线开头的方法