Help us understand the problem. What is going on with this article?

NotificationCompatにhttpから取得した画像を入れる

More than 1 year has passed since last update.

NotificationCompat.Builderは端末のPush通知を送る事ができる機能です。
setLargeIcon(Bitmap bitmap)というメソッドがあるのでそれを使って実装します。

AsyncTaskを使って画像URLをHTTPで取得し、
NotificationCompat.BuilderでBitmapを設定したPush通知を送信するクラスを書きます。

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();
    }
  }
}

呼び出します。

new SendNotificationBuilder(getApplicationContext())
    .setTitle(title)
    .setText(text)
    .setImageUrl(imageUrl)
    .setIntent(intent)
    .execute();
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away