티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
NotificationCompat.Builder deprecated in Android O
new NotificationCompat.Builder(context)는 안드로이드 O에서 더 이상 사용되지 않습니다.
문제 내용
After upgrading my project to Android O
저는 안드로이드 O로 프로젝트를 업그레이드한 후... 라는 뜻입니다.
buildToolsVersion "26.0.1"
Lint in Android Studio is showing a deprecated warning for the follow notification builder method:
Android Studio에서 Lint는 다음 알림 빌더 메소드에 대해 deprecated 경고를 표시합니다:
new NotificationCompat.Builder(context)
The problem is: Android Developers update their Documentation describing NotificationChannel to support notifications in Android O, and provide us with a snippet, yet with the same deprecated warning:
문제는 안드로이드 개발자들이 Android O에서 알림을 지원하기 위해 NotificationChannel을 설명하는 문서를 업데이트하고 코드 스니펫을 제공했지만, 여전히 동일한 Deprecated 경고가 포함되어 있다는 것입니다.
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
알림 개요
My question: Is there is any other solution for building notification, and still support Android O?
내 질문: Android O를 지원하면서 알림을 작성하는 다른 해결책이 있나요?
A solution I found is to pass the channel ID as a parameter in Notification.Builder constructor. But this solution is not exactly reusable.
내가 찾은 해결책은 Notification.Builder 생성자에 채널 ID를 매개변수로 전달하는 것입니다. 그러나 이 해결책은 완전히 재사용 가능한 것은 아닙니다.
new Notification.Builder(MainActivity.this, "channel_id")
높은 점수를 받은 Solution
It is mentioned in the documentation that the builder method NotificationCompat.Builder(Context context)
has been deprecated. And we have to use the constructor which has the channelId
parameter:
문서에는 NotificationCompat.Builder(Context context) 메서드가 더 이상 사용되지 않으며 channelId 매개변수가 있는 생성자를 사용해야한다고 언급되어 있습니다.
NotificationCompat.Builder(Context context, String channelId)
NotificationCompat.Builder Documentation:
NotificationCompat.Builder 문서:
This constructor was deprecated in API level 26.0.0-beta1. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.
이 생성자는 API 레벨 26.0.0-beta1에서 사용이 중지되었습니다. 대신에 NotificationCompat.Builder(Context, String)을 사용하십시오. 모든 게시된 알림은 NotificationChannel ID를 지정해야 합니다.
Notification.Builder Documentation:
Notification.Builder 문서:
This constructor was deprecated in API level 26. use Notification.Builder(Context, String) instead. All posted Notifications must specify a NotificationChannel Id.
이 생성자는 API 레벨 26에서 사용 중지되었습니다. 대신 Notification.Builder(Context, String)를 사용하세요. 모든 게시된 알림은 NotificationChannel ID를 지정해야 합니다.
If you want to reuse the builder setters, you can create the builder with the channelId
, and pass that builder to a helper method and set your preferred settings in that method.
만약 빌더(setter)를 재사용하고 싶다면, channelId로 빌더를 만들고, 해당 빌더를 도우미(helper) 메소드에 전달하여 원하는 설정을 그 메소드에서 설정할 수 있습니다.
가장 최근 달린 Solution
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
Right code will be :
"올바른 코드는 다음과 같습니다:"
Notification.Builder notification=new Notification.Builder(this)
with dependency 26.0.1 and new updated dependencies such as 28.0.0.
26.0.1 버전과 28.0.0과 같은 새로 업데이트된 종속성을 사용하는 경우
Some users use this code in the form of this :
어떤 사용자들은 이 코드를 다음과 같은 형태로 사용합니다:
Notification notification=new NotificationCompat.Builder(this)//this is also wrong code.
So Logic is that which Method you will declare or initilize then the same methode on Right side will be use for Allocation. if in Leftside of = you will use some method then the same method will be use in right side of = for Allocation with new.
그래서 로직은 왼쪽에 선언하거나 초기화한 메소드가 있다면, 오른쪽에는 같은 메소드가 할당에 사용된다는 것입니다. 왼쪽의 = 기호 왼쪽에서 어떤 메소드를 사용한다면, = 기호 오른쪽에서는 new를 사용하여 할당할 때 동일한 메소드를 사용해야 합니다.
Try this code...It will sure work
이 코드를 시도해보세요...확실히 작동할 것입니다.
출처 : https://stackoverflow.com/questions/45462666/notificationcompat-builder-deprecated-in-android-o
'개발 > 안드로이드' 카테고리의 다른 글
Nested RecyclerView에서 wrap_content가 동작하지 않을 때 문제 해결하기 (0) | 2023.02.09 |
---|---|
텍스트를 공유하는 모든 앱에 인텐트를 통해 텍스트 공유하기 (0) | 2023.02.08 |
리스트뷰에서 ArrayIndexOutOfBoundsException 오류 수정하기 (0) | 2023.02.08 |
클래스를 상속하였을 때 인플레이트 오류 수정하기 (0) | 2023.02.07 |
Environment.getExternalStorageDirectory() 대신 내부 저장소 디렉토리 가져오기 (0) | 2023.02.07 |