[Android/Kotlin] Firebase FCM 푸시알림

SoyoungLee·2021년 6월 30일
0

안드로이드/코틀린

목록 보기
9/68
post-thumbnail

💌안드로이드 Firebase FCM 푸시알림

FCM 이란 ❓

Firebase Cloud Messaging

유형

  • Notification : 앱이 포그라운드일 때 (앱이 실행 중일 때)만 푸시알림
  • Data : 포그라운드/백그라운드 상관없이 푸시알림

전송 대상

  • 1명
  • 여러 명 (그룹)
    : 사용자들을 그룹에 가입시켜서 메시지를 보내는 방법 (최대 20명)
  • 여러 명 (구독자)
    : 특정 주제를 구독하는 여러 기기에 메시지 보내는 방법 (1000명까지 가능) 지연 시간이 아닌 처리량을 위주

📌 1. SDK 설정

앱에서 다른 Firebase 기능을 사용 설정한 경우 이미 완료되었을거임

FCM 푸시알림 - 앱수준의 gradle

dependencies {
    // Import the BoM for the Firebase platform
    implementation platform('com.google.firebase:firebase-bom:28.1.0')

    // Declare the dependencies for the Firebase Cloud Messaging and Analytics libraries
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

FCM 푸시알림 - 프로젝트 수준의 gradle

buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }
  dependencies {
    ...
    
    classpath 'com.google.gms:google-services:4.3.8'
    
  }
}

allprojects {
  ...
  
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    ...
    
  }
}

📌 2. 앱 매니페스트 수정

<service
            android:name=".MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

MyFirebaseMessagingService 파일 추가해줘야 함

class MyFirebaseMessagingService: FirebaseMessagingService() {

    private val TAG = "FirebaseService"


    // 토큰 생성
    override fun onNewToken(token: String) {
        Log.d(TAG, "Refreshed token: $token")

        // 토큰 값 따로 저장
        val pref = this.getSharedPreferences("token", Context.MODE_PRIVATE)
        val editor = pref.edit()
        editor.putString("token",token).apply()
        editor.commit()

        Log.i("로그", "토큰 저장 성공적")
    }


    // 메시지 수신
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.from)

        if(remoteMessage.data.isNotEmpty()){
            Log.i("바디", remoteMessage.data["body"].toString())
            Log.i("타이틀",remoteMessage.data["title"].toString())
            sendNotification(remoteMessage)
        }
        else {
            Log.i("수신에러 : ", "data가 비어있습니다. 메시지를 수신하지 못했습니다.")
            Log.i("data값 :",remoteMessage.data.toString())
        }
    }


    // 알림 생성 (아이콘, 알림 소리 등)
    private fun sendNotification(remoteMessage: RemoteMessage){
        // RemoteCode, ID를 고유값으로 지정하여 알림이 개별 표시 되도록 함
        val uniId: Int = (System.currentTimeMillis() / 7).toInt()

        // 일회용 PendingIntent
        // PendingIntent : Intent 의 실행 권한을 외부의 어플리케이션에게 위임
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) // Activity Stack을 경로만 남김, A-B-C-D-B => A-B
        val pendingIntent = PendingIntent.getActivity(this, uniId, intent, PendingIntent.FLAG_ONE_SHOT)

        // 알림 채널 이름
        val channelId = getString(R.string.firebase_notification_channel_id)

        // 알림 소리
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        // 알림에 대한 UI 정보와 작업을 지정
        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)     // 아이콘 설정
            .setContentTitle(remoteMessage.data["body"].toString())     // 제목
            .setContentText(remoteMessage.data["title"].toString())     // 메시지 내용
            .setAutoCancel(true)
            .setSound(soundUri)     // 알림 소리
            .setContentIntent(pendingIntent)       // 알림 실행 시 Intent

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // 오레오 버전 이후에는 채널이 필요
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val channel = NotificationChannel(channelId, "Notice", NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }

        // 알림 생성
        notificationManager.notify(uniId, notificationBuilder.build())

    }
}
profile
Android Developer..+ iOS 슬쩍 🌱 ✏️끄적끄적,,개인 기록용 👩🏻‍💻

0개의 댓글