Kotlin

인스타 클론코딩(8)

hvv_an 2021. 2. 14. 16:10

알람 로그 만들기

어플에서 좋아요가 눌렸거나 댓글이 달렸거나 혹은 팔로우가 됐다면 유저에게 알람을 보내주는 기능을 만들어 보자.
그렇기 하기 위해서는 알람의 log를 남겨 놓는 작업이 필요하다.
따라서 각 Activity마다 log를 firebase에 남겨 놓는 함수를 만들어보자.

우선 AlarmDTO라는 모델을 정의하여 표준화해보자.

package com.example.firstapp.navigation.model


data class AlarmDTO(
        var destinationUid: String? = null,
        var userId: String? = null,
        var uid: String? =null,
        var kind: Int? = null,
        var message: String? = null,
        var timestamp: Long? =null
)

kind는 알람의 종류이다. 0좋아요, 1댓글, 2팔로우이다.

 

우선 좋아요가 눌렸을 때 알람이다.

        fun favoriteAlarm(destinationUid: String){
            var alarmDTO = AlarmDTO()
            alarmDTO.destinationUid = destinationUid
            alarmDTO.userId = FirebaseAuth.getInstance().currentUser?.email
            alarmDTO.uid = FirebaseAuth.getInstance().currentUser?.uid
            alarmDTO.kind = 0
            alarmDTO.timestamp = System.currentTimeMillis()
            FirebaseFirestore.getInstance().collection("alarms").document().set(alarmDTO)
        }

uid를 매개변수로 받아와 목표 uid를 할당한 뒤, 다른 부가 정보를 할당한다.
그런 다음 FirebaseFirestroe에 등록한다.

위의 함수를 좋아요가 안눌러진 상태에서 클릭되었을 때 실행되게 한다.

 

 

다음으로는 댓글 알람을 만들어 보자.

    fun commentAlarm(destinationUid: String, message: String){
        var alarmDTO = AlarmDTO()
        alarmDTO.destinationUid = destinationUid
        alarmDTO.userId = FirebaseAuth.getInstance().currentUser?.email
        alarmDTO.uid = FirebaseAuth.getInstance().currentUser?.uid
        alarmDTO.kind = 1
        alarmDTO.timestamp = System.currentTimeMillis()
        alarmDTO.message = message
        FirebaseFirestore.getInstance().collection("alarms").document().set(alarmDTO)
    }

데이터들을 할당 한 뒤, 등록하는 과정은 동일하다.
한 가지 다른 점은 댓글은 댓글의 내용을 message로 등록한다는 점이다.

그리고 CommentActivity에서 destinationUid는 intent에서 받아오게 한다.

        contentUid = intent.getStringExtra("contentUid")
        destinationUid = intent.getStringExtra("destinationUid")
            //comment
            detailViewItem_comment_imageView.setOnClickListener { v ->
                var intent = Intent(v.context, CommentActivity::class.java)
                intent.putExtra("contentUid",contentUidList[position])
                intent.putExtra("destinationUid",contentDTOs[position].uid)
                startActivity(intent)
            }

(DetailViewActivity의 내용)

 

 

마지막으로 팔로우 알람을 만들어보자.

    fun followerAlarm(destinationUid: String){
        var alarmDTO = AlarmDTO()
        alarmDTO.destinationUid = destinationUid
        alarmDTO.userId = auth?.currentUser?.email
        alarmDTO.uid = auth?.currentUser?.uid
        alarmDTO.kind = 2
        alarmDTO.timestamp = System.currentTimeMillis()

        FirebaseFirestore.getInstance().collection("alarms").document().set(alarmDTO)
    }

다른 알람과 다른 점은 없다.
다른 사람의 계정으로 들어가 팔로우 버튼을 눌렀을 때 해당 함수가 실행되게 하면 된다.

//save data to third person
        var tsDocFollower = firestore?.collection("users")?.document(uid!!)
        firestore?.runTransaction { transition ->
            var followDTO = transition.get(tsDocFollower!!).toObject(FollowDTO::class.java)
            if(followDTO == null){
                followDTO = FollowDTO()
                followDTO!!.followerCount = 1
                followDTO!!.followers[currentUserUid!!] = true
                followerAlarm(uid!!)

                transition.set(tsDocFollower, followDTO!!)
                return@runTransaction
            }else{
                if(followDTO!!.followers.containsKey(currentUserUid!!)){
                    followDTO!!.followerCount = followDTO!!.followerCount - 1
                    followDTO!!.followers.remove(currentUserUid!!)
                }else{
                    followDTO!!.followerCount = followDTO!!.followerCount + 1
                    followDTO!!.followers[currentUserUid!!] = true
                    followerAlarm(uid!!)
                }

                transition.set(tsDocFollower, followDTO!!)
                return@runTransaction
            }
        }