Put the image obtained from http in NotificationCompat

NotificationCompat.Builder is a function that can send push notifications of the terminal. Since there is a method called setLargeIcon (Bitmap bitmap), implement it using it.

Use AsyncTask to get the image URL over HTTP Write a class to send push notifications with Bitmap set in NotificationCompat.Builder.

class SendNotificationBuilder extends AsyncTask<String, Void, Bitmap> {

  private String title;
  private String message;
  private String imageUrl;
  private Intent intent;

  public SendNotificationBuilder(Context context) {
    super();
    this.context = context;
  }

  public SendNotificationBuilder setTitle(String title) {
    this.title = title;
    return this;
  }

  public SendNotificationBuilder setMessage(String message) {
    this.message = message;
    return this;
  }

  public SendNotificationBuilder setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
    return this;
  }

  public SendNotificationBuilder setIntent(Intent intent) {
    this.intent = intent;
    return this;
  }

  @Override
  protected Bitmap doInBackground(String... params) {
      if (imageUrl == null) return null;
      InputStream in;
      try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        in = connection.getInputStream();
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        return bitmap;
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return null;
  }

  @Override
  protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    try {
      NotificationManagerCompat manager = 
          NotificationManagerCompat.from(getApplicationContext());
      NotificationCompat.Builder builder;
      builder = new NotificationCompat.Builder(context)
          .setContentTitle(title)
          .setContentText(text);

      builder.setSmallIcon(R.drawable.icon_app);
      builder.setColor(ContextCompat.getColor(context, R.color.white));
      if (bitmap != null) {
        builder.setLargeIcon(bitmap);
      }
      builder.setAutoCancel(true);

      PendingIntent contentIntent = PendingIntent.getActivity(
          getApplicationContext(), REQUEST_ID,
          intent, PendingIntent.FLAG_ONE_SHOT);
      builder.setContentIntent(contentIntent);
      manager.notify(NOTIFY_ID, builder.build());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Call.

new SendNotificationBuilder(getApplicationContext())
    .setTitle(title)
    .setText(text)
    .setImageUrl(imageUrl)
    .setIntent(intent)
    .execute();

Recommended Posts

Put the image obtained from http in NotificationCompat
Draw screen with Thymeleaf in SpringBoot
Put the image obtained from http in NotificationCompat
JavaFX-Load Image in the background
[HTTP] Status code included in the HTTP response
Rails6: Extract the image in Action Text
Display the background image in the production environment
[Android development] Get an image from the server in Java and set it in ImageView! !!
Get the URL of the HTTP redirect destination in Java
Read the packet capture obtained by tcpdump in Java
Tokoro I rewrote in the migration from Wicket 7 to 8
Get the URL of the HTTP redirect destination in Ruby
[Rails] How to display an image in the view