Skip to content

Commit

Permalink
[Feat/#1] FCM 알림 포그라운드, 클릭시 화면이동 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ss99x2002 committed Jun 29, 2023
1 parent 5ee7b7f commit 6d73fd5
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:exported="false"/>
<service android:name=".util.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
Expand All @@ -37,7 +39,7 @@
android:resource="@drawable/ic_launcher_foreground"/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/black"/>
android:resource="@color/orange"/>
</application>

</manifest>
11 changes: 11 additions & 0 deletions app/src/main/java/com/example/practice_android/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.practice_android

import android.os.Bundle
import com.example.practice_android.databinding.ActivitySecondBinding
import com.example.practice_android.util.binding.BindingActivity

class SecondActivity : BindingActivity<ActivitySecondBinding>(R.layout.activity_second) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_MUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
Expand All @@ -13,6 +14,7 @@ import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.practice_android.MainActivity
import com.example.practice_android.R
import com.example.practice_android.SecondActivity
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingService
Expand All @@ -28,8 +30,41 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
super.onMessageReceived(message)
Log.e("hyeon",message.notification.toString())
Log.e("hyeon",message.data.toString())
createNotification(message)
}

private fun createNotification(message: RemoteMessage){
val intent = Intent(this,SecondActivity::class.java)
for (key in message.data.keys) {
intent.putExtra(key,message.data.get(key))
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this,(System.currentTimeMillis()/7).toInt(),intent,PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE)

val notificationChannel = NotificationChannel(
CHANNEL_ID,
"TestChannelName",
NotificationManager.IMPORTANCE_DEFAULT
)

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Title")
.setContentText("ContentText")
.setContentIntent(pendingIntent)

val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)

notificationManager.notify(
1, // 해당 알림의 고유 ID
builder // 표시할 알림
.setContentTitle("Title")
.setContentText("ContentText")
.build()
)
}
private fun getFcmToken(){
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener{ task->
if(!task.isSuccessful){
Expand All @@ -40,4 +75,8 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
Log.e("hyeon", "token is $token")
})
}
companion object {
private const val CHANNEL_ID = "channel_id"
private const val notificationId = 1234
}
}
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="알림 클릭시 나오는 뷰"
android:textColor="@color/black"
android:textSize="21sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="orange">#FF6600</color>
</resources>

0 comments on commit 6d73fd5

Please sign in to comment.