반응형
1. Android Studio 코드 추가
1-1. MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
FirebaseApp.initializeApp(this@MainActivity)
firebaseToken()
}
private fun firebaseToken() {
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w("FCM Test: ", "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
val token = task.result
Log.d("FCM Test: ", token) //자신의 토큰 확인.
})
}
1-2. FirebaseMessageService 클래스 추가 .
class FirebaseMessageService : FirebaseMessagingService() {
companion object {
private const val TAG = "FCM Test: "
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "Message Notification Body: ${remoteMessage.data}")
remoteMessage.data.isNotEmpty().let {
Log.d(TAG, "Message Notification Body: ${remoteMessage.data}")
val title = remoteMessage.data["title"]
val body = remoteMessage.data["body"]
showNotification(title, body)
}
remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
showNotification(it.title, it.body)
}
}
private fun showNotification(title: String?, message: String?) {
val channelId = getString(R.string.notification_channel_id)//자신이 설정할 채널 id
val channelName = getString(R.string.notification_channel_name) // 자신이 설정할 채널 이름
val notificationId = 0
val notificationManager: NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager =
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val channel =
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
} else {
notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
}
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
}
val pendingIntent: PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.getActivity(
this,
System.currentTimeMillis().toInt(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
} else {
PendingIntent.getActivity(
this,
System.currentTimeMillis().toInt(),
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background) // 알림 아이콘
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
notificationManager.notify(notificationId, notificationBuilder.build())
}
override fun onNewToken(refreshedToken: String) {
super.onNewToken(refreshedToken)
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.e(TAG, task.result)
}
}
}
}
fun Activity.getIntentNotification(): Intent {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent().apply {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
}
} else {
Intent().apply {
action = "android.settings.APP_NOTIFICATION_SETTINGS"
putExtra("app_package", packageName)
putExtra("app_uid", applicationInfo.uid)
}
}
}
2. Firebase 에서 FCM 보내보기.
2-1. Firebase 프로젝트 -> Messaging 클릭.
2-2. MainActivity 안의 firebaseToken () 에서 확인한 token 확인.
2-3. Messaging 에서 테스트 메세지 전송 클릭 후 토큰 추가 .
확인.
반응형
'안드로이드' 카테고리의 다른 글
[Android Studio] Firebase FCM 사용법 . Part. 1 (0) | 2024.12.11 |
---|---|
[Android Studio] SHA1 확인하는 방법. (0) | 2024.10.14 |
[Android Studio] Drop down 사용하기 (0) | 2024.04.12 |
[Android Studio] ScrollView 방향 확인 법 응용. (코틀린) (0) | 2024.03.23 |
[Android Studio] StatusBar 색상 변경하기 (코틀린) (0) | 2024.03.21 |