728x90
반응형
1. Notification XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="120dp">
<ImageView
android:id="@+id/profile_image"
android:layout_gravity="center"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginLeft="40dp"
android:scaleType="centerCrop"
android:src="@drawable/test"
tools:ignore="NestedWeights,RtlHardcoded" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="45dp"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:weightSum="2">
<TextView
android:id="@+id/target_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="13sp"
android:textColor="#000000"
android:gravity="top"
android:text="이븐이"
tools:ignore="HardcodedText,SmallSp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textSize="10sp"
android:textColor="#000000"
android:text="안녕하세요? 테스트중입니다!"
tools:ignore="HardcodedText,SmallSp" />
</LinearLayout>
</LinearLayout>
이미지 경로 : res -> drawable
2. JAVA
package com.example.mydestinyfouuser.MainActivity;
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.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.RemoteViews;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.NotificationTarget;
import com.example.mydestinyfouuser.R;
public class MainActivity extends AppCompatActivity{
private String NOTIFICATION_CHANNEL_ID = "MYCHANEL"; //채널명
private int notificationId; // 노티피케이션 식별자
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Notification_TEST();
}
public void Notification_TEST() {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, Video_Call_Connecting.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK) ;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.video_call_notification);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground)) //BitMap 이미지 요구
.setContentTitle("Title")
.setContentText("Content")
.setContent(contentView)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent) // 사용자가 노티피케이션을 탭시 이 Activity 로 되돌아옴
.setAutoCancel(true);
//OREO API 26 이상부터는 채널이 필요하다.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setSmallIcon(R.drawable.ic_launcher_foreground);
String description = "ForOreo";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_ID , importance);
channel.setDescription(description);
// 노티피케이션 채널을 시스템에 등록
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}else{
builder.setSmallIcon(R.mipmap.ic_launcher); // Oreo 이하에서 mipmap 사용하지 않으면 Couldn't create icon: StatusBarIcon 에러남
}
/* 원본사진의 가로 세로 비율에 따라서 ScaleType 조정 ( CenterCrop , CenterInside ) */
notificationId = (int)System.currentTimeMillis(); //노티 식별자 ID
NotificationTarget notificationTarget = new NotificationTarget(this.getApplicationContext(), R.id.profile_image, contentView, builder.build(), notificationId); //노티 내부에 Glide 를 적용할 Target View
Glide.with(this.getApplicationContext()).asBitmap().circleCrop().load(R.drawable.test).into(notificationTarget);//위에서 가져온 View 에 Glide 를 적용해서 원형태로 만든다.
notificationManager.notify(notificationId, builder.build()); // 노티 식별자로 노티 작동
}
}
728x90
반응형
'Android' 카테고리의 다른 글
[Android] Kotlin, Java 홈버튼(최소화) 이벤트 (0) | 2021.11.17 |
---|---|
[Kotlin] ViewPager2 code (0) | 2021.09.14 |
댓글