У меня есть служба уведомлений с пользовательскими раскладками. Я использую 2 макета, один меньший, а другой расширенный вид. Я загружаю изображение с URL через Glide и настраиваюсь в расширенном представлении. Я хочу отобразить пользовательский вид, но расширенный вид должен отображаться только тогда, когда изображение в глиссаде было успешно загружено, иначе расширенный вид не должен отображаться.
NotifiationService. java
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.RemoteViews;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.NotificationTarget;
import com.bumptech.glide.request.target.Target;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebaseNotificationService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
String newsTitle = remoteMessage.getData().get("t");
String newsURL = remoteMessage.getData().get("u");
String newsImageURL = remoteMessage.getData().get("ui");
sendNotification(newsTitle, newsURL, newsImageURL);
}
}
private void sendNotification(final String newsTitle, String newsURL, String newsImageURL) {
final String channelId = getString(R.string.default_notification_channel_id);
final int id = (int) System.currentTimeMillis();
final RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.expanded_notification_layout);
expandedView.setTextViewText(R.id.notificationText, newsTitle);
expandedView.setImageViewResource(R.id.notificationImage, R.drawable.news_default);
final RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.collapsed_notification_layout);
collapsedView.setTextViewText(R.id.notificationText, newsTitle);
Intent intent = new Intent(this, SplashScreenActivity.class);
intent.putExtra("newsURL", newsURL);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
.setVibrate(new long[]{1000,1000})
.setContentIntent(pendingIntent);
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Headlines",
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.enableVibration(true);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
Notification notification = builder.build();
assert notificationManager != null;
notificationManager.notify(id, notification);
NotificationTarget notificationTarget = new NotificationTarget(
getApplicationContext(), R.id.notificationImage, expandedView, notification, id);
Glide.with(getApplicationContext())
.asBitmap()
.load(newsImageURL)
.apply(RequestOptions.bitmapTransform(new RoundedCorners(16)))
.into(notificationTarget);
}
}
Просмотр свернутых уведомлений
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@android:color/white"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="@+id/notificationIcon"
android:layout_width="56dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:padding="4dp"
android:src="@drawable/notification_icon"/>
<TextView
android:id="@+id/notificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/notificationIcon"
android:paddingRight="8dp"
android:layout_centerInParent="true"
android:maxLines="3"
android:textStyle="bold"
android:ellipsize="end"
tools:text="This is an sample notification text which is being displayed here."
android:layout_marginLeft="8dp"/>
</RelativeLayout>
Расширенный просмотр уведомлений
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="256dp">
<ImageView
android:id="@+id/notificationImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:src="@drawable/news_default"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/notificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="This is the notification which will appear here."
android:textColor="@android:color/white"
android:layout_alignBottom="@id/notificationImage"
android:layout_marginBottom="10dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textStyle="bold"
android:textSize="14sp"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"/>
</RelativeLayout>