먼저, 아래에 있는 google-services.json 파일을 넣는다.

build.gradle의 Project Level과 Module Level 모두 dependency를 추가해야 한다.
plugins {
id 'com.google.gms.google-services' version '4.3.15' apply false
}
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
dependencies {
implementation platform('com.google.firebase:firebase-bom:32.2.3')
implementation 'com.google.firebase:firebase-messaging:23.2.1'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
AndroidManifest.xml 파일 설정을 아래처럼 추가한다.
<uses-permission android:name="android.permission.INTERNET"/>
토큰 생성 코드 작성을 아래처럼 한다.
**public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.w("FCM Log", "Fetching FCM registration token failed", task.getException());
return;
}
String token = task.getResult();
Log.d("FCM Log", "Current token: " + token);
});
}
}**
토큰 생성이 제대로 되었는지 TEST를 해본다.

이제, 서버 쪽에서 FCM을 통해 메시지를 전송하면 클라이언트 쪽에서는 받아야 하므로, Message receive 관한 Service 코드 작성이 필요하다.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String NOTIFICATION_CHANNEL_ID = "sample.noti.app";
private static final String NOTIFICATION_CHANNEL_NAME = "Notification";
private static final String NOTIFICATION_CHANNEL_DESCRIPTION = "notification channel";
// Refreshed Token 로그 확인 메서드
@Override
public void onNewToken(@NonNull String token) {
Log.d("FCM Log", "Refreshed token: " + token);
}
// FCM을 통해 디바이스에 메시지가 도착하면 동작하는 메서드
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
// 여기서는 메시지의 데이터 값 "content"를 꺼낸다.
String content = remoteMessage.getData().get("content");
showNotification(content);
}
}
// 실질적으로 디바이스에 알림이 푸쉬될 수 있도록 하는 메서드
private void showNotification(String content) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(notificationManager);
}
// 키워드 알림이기 때문에, 먼저 알림 테스트 후 수정 필요
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("A new post you might be interested in.")
.setContentText(content)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());
}
//
private void createNotificationChannel(NotificationManager manager) {
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(notificationChannel);
}
}
}
AndroidManifest.xml 서비스 등록하기
<service android:name=".MyFirebaseMessagingService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>