안드로이드 푸시를 받아보자.
4. 이제 나온 가이드대로 해봅시다.
구글 계정은 있어야 하고, 기본적인 안드로이드는 알고 있어야 합니다.
최신 안드로이드 스튜디오에서 FCM 연결을 제공함으로써 간단하게 된다.
최대한 간단하게 해보려고 합니다.
-- 가이드 확인 --
1. 푸시를 받아볼 샘플 프로젝트를 만듭니다.
2. 상단메뉴 Tools -> Firebase 클릭합니다.
3. 우측에 나온 메뉴에서 Cloud Message 선택
-- 설정 --
1. Connect your app to Firebase
"Connect to Firebase" 버튼 클릭 후, 구글 로그인
2. 첫번째 "Create New Firebase project"를 선택 후 "Connect to Firebase"3. 그럼 가이드에 체크 된다.
4. Add FCM to your app 을 해보자 "Add FCM to your app" 을 클릭한다.
팝업을 클릭해주면 Gradle에 푸시를 받기위한 플러그인이 들어간다.
7. 프로젝트에 넣어준다.
8. 이제 서비스를 만들자.
<service서비스 소스.
android:name=".MyService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
package com.example.loginactivity토큰을 알아둬야 한다. 앱 실행시 토큰을 알고 싶으면 아래의 코드를 넣어두자.
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import com.example.loginactivity.ui.login.LoginActivity
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
class MyService : FirebaseMessagingService() {
private val TAG = "MyFirebaseMessaging"
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
sendRegistrationToServer(token)
}
fun sendRegistrationToServer(token: String) {
// 서버에 토큰 전달
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "From: " + remoteMessage.from);
if (remoteMessage.data.isNotEmpty()) {
Log.d(TAG, "Message data payload: " + remoteMessage.data);
// 메세지 수신처리
val title: String? = remoteMessage.data["title"]
val message: String? = remoteMessage.data["message"]
val intent = Intent(this, LoginActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = "채널"
val channel_nm = "채널명"
val notichannel =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelMessage = NotificationChannel(
channel, channel_nm,
NotificationManager.IMPORTANCE_DEFAULT
)
channelMessage.description = "채널에 대한 설명."
channelMessage.enableLights(true)
channelMessage.enableVibration(true)
channelMessage.setShowBadge(false)
channelMessage.vibrationPattern = longArrayOf(1000, 1000)
notichannel.createNotificationChannel(channelMessage)
//푸시알림을 Builder를 이용하여 만듭니다.
val notificationBuilder =
NotificationCompat.Builder(this, channel)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title) //푸시알림의 제목
.setContentText(message) //푸시알림의 내용
.setChannelId(channel)
.setAutoCancel(true) //선택시 자동으로 삭제되도록 설정.
.setContentIntent(pendingIntent) //알림을 눌렀을때 실행할 인텐트 설정.
.setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(9999, notificationBuilder.build())
} else {
val notificationBuilder =
NotificationCompat.Builder(this, "")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND or Notification.DEFAULT_VIBRATE)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(9999, notificationBuilder.build())
}
}
}
}
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener (
this,
OnSuccessListener<InstanceIdResult> { instanceIdResult ->
val token = instanceIdResult.token
Log.i("MyFirebaseMessaging", token)
})
9. 이제 모든 설정이 완료 되었다.
포스트맨으로 푸시를 쏘아보자.
9-1.서버 인증키 획득 구글콘솔 설정에서 "클라우드 메시징"에 있다.
9-2.전송
POST













댓글
댓글 쓰기