Load image from url in notification Android

前端 未结 8 798
北海茫月
北海茫月 2020-11-28 21:48

In my android application, i want to set Notification icons dynamically which will be loaded from URL. For that, i have used setLargeIcon property of Notificati

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 22:16

    How to implement BigPicture style Notification:

    Miracle has been done by .setStyle(new Notification.BigPictureStyle().bigPicture(result)) :

    I have done this way with:

    Generate notification by AsyncTask:

    new generatePictureStyleNotification(this,"Title", "Message", 
                     "http://api.androidhive.info/images/sample.jpg").execute();
    

    AsyncTask:

    public class generatePictureStyleNotification extends AsyncTask {
    
            private Context mContext;
            private String title, message, imageUrl;
    
            public generatePictureStyleNotification(Context context, String title, String message, String imageUrl) {
                super();
                this.mContext = context;
                this.title = title;
                this.message = message;
                this.imageUrl = imageUrl;
            }
    
            @Override
            protected Bitmap doInBackground(String... params) {
    
                InputStream in;
                try {
                    URL url = new URL(this.imageUrl);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    in = connection.getInputStream();
                    Bitmap myBitmap = BitmapFactory.decodeStream(in);
                    return myBitmap;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
    
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
    
                Intent intent = new Intent(mContext, MyOpenableActivity.class);
                intent.putExtra("key", "value");
                PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 100, intent, PendingIntent.FLAG_ONE_SHOT);
    
                NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notif = new Notification.Builder(mContext)
                        .setContentIntent(pendingIntent)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(result)
                        .setStyle(new Notification.BigPictureStyle().bigPicture(result))
                        .build();
                notif.flags |= Notification.FLAG_AUTO_CANCEL;
                notificationManager.notify(1, notif);
            }
        }
    

提交回复
热议问题