Android

[Android] Firebase Push Token (feat. WebView)

김한토 2025. 2. 19. 11:28
반응형

https://firebase.google.com/docs/cloud-messaging/android/first-message?hl=ko&authuser=0&_gl=1*dvs24r*_ga*MTQyMzQxMTk4My4xNzQxMDYyMjUy*_ga_CW55HF8NVT*MTc0MTA2MjI1MS4xLjEuMTc0MTA2MjM2Ny4xOC4wLjA.#retrieve-the-current-registration-token

 

백그라운드 앱에 테스트 메시지 보내기  |  Firebase Cloud Messaging

4월 9~11일, Cloud Next에서 Firebase가 돌아옵니다. 지금 등록하기 의견 보내기 백그라운드 앱에 테스트 메시지 보내기 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하

firebase.google.com

 

 

https://firebase.google.com/docs/cloud-messaging/android/client?hl=ko

 

Android에서 Firebase 클라우드 메시징 클라이언트 앱 설정  |  Firebase Cloud Messaging

4월 9~11일, Cloud Next에서 Firebase가 돌아옵니다. 지금 등록하기 의견 보내기 Android에서 Firebase 클라우드 메시징 클라이언트 앱 설정 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저

firebase.google.com

 

https://firebase.google.com/docs/cloud-messaging/android/receive?hl=ko

 

Android 앱에서 메시지 수신  |  Firebase Cloud Messaging

4월 9~11일, Cloud Next에서 Firebase가 돌아옵니다. 지금 등록하기 의견 보내기 Android 앱에서 메시지 수신 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Firebase 알

firebase.google.com

 

 

https://velog.io/@simsubeen/Android-Kotlin-Firebase-Cloud-Messaging

 

[Android / Kotlin] Firebase Cloud Messaging - PUSH 알림 받기

📍 Firebase Console을 사용하여 Firebase 추가 > Firebase 프로젝트 만들기 및 Firebase에 앱 등록 하는 부분은 참고할 수 있는 예제가 많기에 본 포스팅에서는 생략하려고 한다. Firebase 문서의 'Firebase 프로

velog.io

 

이렇게 해도 알림이 null로 오는 경우가 있다. 

 

override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        remoteMessage.notification?.let { notification ->
            Log.d(TAG, "Notification Title: ${notification.title}")
            Log.d(TAG, "Notification Body: ${notification.body}")

            // null 체크 후 sendNotification 호출
            val title = notification.title
            val body = notification.body

            if (title != null && body != null) {
                sendNotification(title, body)
            } else {
                Log.w(TAG, "Notification title or body is null, not sending notification.")
            }
        }

        // Data 페이로드 확인
        if (remoteMessage.data.isNotEmpty()) {
            val title = remoteMessage.data["title"].takeUnless { it.isNullOrEmpty() }
                ?: remoteMessage.notification?.title
                ?: "No Title"

            val body = remoteMessage.data["body"].takeUnless { it.isNullOrEmpty() }
                ?: remoteMessage.notification?.body
                ?: "No Body"

            Log.d(TAG, "Received Message - Title: $title, Body: $body")
            sendNotification(title, body)

            Log.d(TAG, "Data Payload - Title: $title, Body: $body")
            sendNotification(title, body)
            return
        }

        // Notification 페이로드 확인
        remoteMessage.notification?.let {
            val title = remoteMessage.data["title"].takeUnless { it.isNullOrEmpty() }
                ?: remoteMessage.notification?.title
                ?: "No Title"

            val body = remoteMessage.data["body"].takeUnless { it.isNullOrEmpty() }
                ?: remoteMessage.notification?.body
                ?: "No Body"

            Log.d(TAG, "Received Message - Title: $title, Body: $body")
            sendNotification(title, body)

            Log.d(TAG, "Notification Payload - Title: $title, Body: $body")
            sendNotification(title, body)
        }

    }

 

이렇게 수정해보자

 

+추가 Android 레벨 13 이상일땐 알림 권한 요청을 보내자, 그러면 앱 다운 받고 유저에게 권한을 물어본다.

 

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
    private val PERMISSIONS = arrayOf(
        Manifest.permission.POST_NOTIFICATIONS
    )

    private val PERMISSION_REQUEST_CODE = 5000


//대충 메인 액티비티 온크리에잍트에
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            if (!Permission.checkAndRequestPermissions(this, PERMISSIONS, PERMISSION_REQUEST_CODE)) {
                Log.d(TAG, "알림 권한 요청함")
            }
        }
반응형