FCM 애널리틱스 연동

May 29, 2024
FCM 애널리틱스 연동
파이어베이스 docs: https://firebase.google.com/?hl=ko
notion image

FCM 설정 하는 방법

  1. 프로젝트 생성후에
  1. 설정 버튼 → 프로젝트 설정 → 클라우드 메시징 → Cloud Messaging API 오른쪽 점 세 개 클릭→ Google Cloud Console에서 API 관리 클릭
notion image
notion image
  1. 사용 버튼 클릭하면 FCM 설정 완료된다.
notion image
  1. 플러터 아이콘 클릭
notion image
  1. 절차에 따라서 진행하면 된다.
notion image
1. sudo npm install -g firebase-tools 2. firebase --version 3. firebase login //firebase logout 은, 로그아웃 하게 됨 4. dart pub global activate flutterfire_cli 5. flutterfire configure --project= ~ // 복사 후에, 붙여넣기
notion image
  1. Android, IOS, macos, web이 나올텐데, 거기서 선택하면 된다.
→ 후에, firebase_options.dart 파일이 생성이 된다.
  1. 해당하는 내용을 미리 복사하기 해둔다.
notion image
  1. main.dart 파일에, 복사한 내용을 넣어야 된다.
import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart'; 추가로, void main() { //추가한 내용, await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp()); }
  1. Widget과 binding을 Interact하게 하기 위해서는, 문자을 추가해줘야 된다.
WidgetsFlutterBinding.ensureInitialized(); Future<void> main() async { //추가한 내용, WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(const MyApp()); }
  1. 프로젝트에서, json 파일을 추가해줘야 된다.
notion image
  1. app 폴더 아래에 google-services.json 추가해줘야 된다. (android→app)
  1. app 폴더 아래에, build.gradle이 있는데, Plugins안에, 값을 넣어줘야 한다.
plugins { // id 'com.google.gms.google-services' // }
  1. android 안에, build.gradle안에, 아래에 코드가 적혀 있을텐데, 이 부분을 수정 후 코드로 변경
//수정 전 코드 allprojects { repositories { google() mavenCentral() } }
//수정 후 코드 buildscript { repositories { google() mavenCentral() } dependencies { classpath 'com.google.gms:google-services:4.3.8' } }
  1. 공식 문서를 살펴보기(FCM) https://firebase.google.com/docs/cloud-messaging?hl=ko
  1. 공식 문서에서, Flutter → Flutter 클라이언트 설정 들어가기
notion image
flutter pub add firebase_messaging
  1. Android, IOS 설정이 아래 나와 있을텐데, 우선 안드로이드 설정부터 진행
💡
src → main → AndroidManifest.xml 들어가서, </activity> 아래 추가하기
notion image
notion image
<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" /> <meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
  1. 아래 부분도 추가해줘야 된다.
notion image
notion image
await FirebaseMessaging.instance.setAutoInitEnabled(true);
 
  1. 메시지 수신 → 수신할 수 있는 권한 내용 추가
 
notion image
main.dart 아래에 내용을 추가하고,
Future<void> requesetNotificationPermission() async { FirebaseMessaging messaging = FirebaseMessaging.instance; NotificationSettings settings = await messaging.requestPermission( alert: true, announcement: false, badge: true, carPlay: false, criticalAlert: false, provisional: false, sound: true, ); print('Notification permission granted : ${settings.authorizationStatus}'); }
requesetNotificationPermission은 함수이므로, main() async 부분에 추가하기
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); //추가해준 부분 requesetNotificationPermission(); await FirebaseMessaging.instance.setAutoInitEnabled(true); FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); } }); final fcmToken = await FirebaseMessaging.instance.getToken(); print('fcm Token값은: $fcmToken'); //runApp(const MyApp()); }
  1. foreground 메세지 처리(main() async 부분에 추가)
notion image
FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); } });
  1. 백그라운드 메시지 처리
notion image
  1. 테스트 메시지 보내기
notion image
final fcmToken = await FirebaseMessaging.instance.getToken(); print('fcm Token값은: $fcmToken');
19. main () async 위에 추가해주기
@pragma('vm:entry-point') Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // If you're going to use other Firebase services in the background, such as Firestore, // make sure you call `initializeApp` before using other Firebase services. await Firebase.initializeApp(); print("Handling a background message: ${message.messageId}"); }
 
 
  1. flutter run 해보기(에뮬레이터를 실행 후, run 해보기)
print('fcm Token값은: $fcmToken');
notion image
  1. 등록할 때, firebase_analytics를 설정했다면, 무조건 등록을 해줘야 확인이 가능하다.
firebase_analytics: ^10.8.10

 
  1. fcm 토큰값을 node.js로 설정하는 방법
 

IOS 설정하는 방법

  1. https://developer.apple.com/ 로그인 하기
  1. 식별자(영문) 클릭
notion image
  1. 클릭해보면, 현재 이미 등록이 되어 있는 상태임
WMU -
notion image
참조할만한 사이트:
 

추가로, IOS에 등록하는 방법
 
 
 
 
 
 
 
Share article

정리한 노션 내용을 올리는 공간