안드로이드

[Android] Custom NotificationView(RemoteViews)에 대해 알아 보자

코딩하는후운 2024. 3. 26. 16:12
반응형

Android Custom NotificationView(RemoteViews)에 대해 알아 보자

Android custom NotificationView를 찾다가 RemoteViews라는게 있었습니다!

notification 을 원하는 형태로 그릴 수 있었습니다!


val acceptIntent = Intent(this, 원하는화면::class.java)
val rejectIntent = Intent(this, 원하는화면::class.java)

RemoteViews(packageName, R.layout.보여줄xml).let {

    val contents = "원하는 내용"
    
    it.setTextViewText(R.id.textview1, "내용")
    it.setTextViewText(R.id.textview2, "내용")
    it.setOnClickPendingIntent(
        R.id.accept_button,
        PendingIntent.getBroadcast(
            this,
            0,
            acceptIntent,
            PendingIntentKt.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
        )
    )
    it.setOnClickPendingIntent(
        R.id.reject_button,
        PendingIntent.getBroadcast(
            this,
            0,
            rejectIntent,
            PendingIntentKt.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
        )
    )



    val notiIntent =
        Intent(this, ConnectingActivity::class.java).addFlags(FLAG_ACTIVITY_NEW_TASK)
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

    val builder = NotificationCompat.Builder(this, 채널ID)
    builder
        .setCustomContentView(it)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setShowWhen(false)
        .setOngoing(true)
        .setContentText(contents)
        .setSmallIcon(R.drawable.ic_notification_red)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setFullScreenIntent(
            PendingIntent.getActivity(
                this,
                0,
                notiIntent,
                PendingIntentKt.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
            ), true
        )
        .setCategory(Notification.CATEGORY_CALL)
        .build()
        .run { startForeground(KINOLINK_NOTIFICATION_ID, this) }

    var image = "image url"
    //노티 내부에 Glide 를 적용할 Target View
    val notificationTarget = NotificationTarget(
        this,
        R.id.target_user_image_view,
        it,
        builder.build(),
        노티피케이션ID
    )
    //위에서 가져온 View 에 Glide 를 적용해서 원형태로 만든다
    Glide
        .with(this)
        .asBitmap()
        .load(imageSmall)
        .into(notificationTarget)

}

xml

ConstraintLayout은 지원하지 않았던 걸로 기억합니다!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="32dp"
    android:paddingHorizontal="16dp"
    android:paddingVertical="8dp">

    <ImageView
        android:id="@+id/notification_icon_image_view"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="8dp"
        android:src="@drawable/ic_notification_red"
        tools:ignore="contentDescription" />

    <TextView
        android:id="@+id/tvNotificationContents"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/notification_icon_image_view"
        android:ellipsize="end"
        android:lineSpacingExtra="0dp"
        android:singleLine="true"
        android:text="@string/connecting_notification_calling_contents"
        android:textColor="@color/notification_main_color"
        android:textSize="12dp"
        tools:ignore="SpUsage" />
</RelativeLayout>

 

참조 : https://stackoverflow.com/questions/21925688/adding-button-action-in-custom-notification

반응형