4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】Notification(通知)でGlideを使ってhttps経由の画像を表示させる

Last updated at Posted at 2017-11-08

RemoteViewクラスのsetImageViewUri(int viewId, Uri uri)
では、できないのでメモ。

Glide 4.1.1 を使用しました。

//☆まずはNotificationを作る =========================================

//NotificationManagerを取得
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

//Intentを生成
Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

//カスタムレイアウトを使う場合は、RemoteViewsを使用する
//今回は、TextView ×2 と ImageViewのレイアウト
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.title, "タイトル");
contentView.setTextViewText(R.id.body, "サブテキスト");

//Notification インスタンスを生成
Notification notification = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("Ticker")
                .setPriority(Notification.PRIORITY_MAX)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setContent(contentView)
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .build();

//idはなんでも良い
manager.notify(1, notification);

//☆ここからがGlideの処理 =========================================

//NotificationTarget インスタンスを生成する
NotificationTarget notificationTarget = new NotificationTarget(
                context,
                R.id.image,
                contentView,
                notification,
                1);

//StringのURLをUriクラスにパースする
Uri uri = Uri.parse("https://〜");

//Gride 4.1.1
Glide.with(getApplicationContext())
               .asBitmap()
               .load(uri)
               .into( notificationTarget );

GlideのNotificationTargetクラスを使用して、
RemoteViewsのカスタムレイアウトにあるImageViewに画像を表示させることができました :beer:

参考:
Glide--Loading Images into Notifications

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?