[Flutter] Flutter FCM 세팅 (푸시 알림) - 2

soonmuu·2023년 2월 22일

flutter 푸시알림

목록 보기
2/3
post-thumbnail

전체적인 흐름

  1. firebase 프로젝트 생성
  2. 패키지 설치
  3. android, ios 세팅
  4. flutter 코드 추가
  5. firebase에서 테스트 메시지로 알림 테스트

2. 패키지 설치

  firebase_core: ^2.6.1
  firebase_messaging: ^14.2.4
  flutter_local_notifications: ^13.0.0 // 앱이 켜져있을때 알림을 위해 사용

https://pub.dev/packages/firebase_core
https://pub.dev/packages/firebase_messaging
https://pub.dev/packages/flutter_local_notifications

3-1. android 세팅

android\app\src\main\AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
  ...
  	<!--추가-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />   
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
  
  	<application
        android:label="myapp"
     ... >
      
      <!--추가-->
      <!-- [START fcm_default_channel] -->
        <meta-data
           android:name="firebase_messaging_auto_init_enabled"
           android:value="false" />
        <meta-data
           android:name="firebase_analytics_collection_enabled"
           android:value="false" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="high_importance_channel" />
      <!-- [END fcm_default_channel] -->
      <activity
            android:name=".MainActivity"
      		...
            <!--추가 -->
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            >
        <!--알림 클릭시 메인이 열리는 intent -->
       <intent-filter>
         <action android:name="android.intent.action.MAIN"/>
         <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      
      ...
      

android/app/build.gradle flutter_local_notifications readme 문서 참고
!! 디슈가링 사용시 Android 12L 이상 버전에서 앱충돌현상이 일어날 수 있음

android {
 	compileSdkVersion 33  // compileSdkVersion 버전 33으로 설정
 
  defaultConfig {
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
  
  // 디슈가링 충돌 보완
  implementation 'androidx.window:window:1.0.0'
  implementation 'androidx.window:window-java:1.0.0'
}

android/build.gradle Gradle plugin 4.2.2 이상 사용

buildscript {
   ...

    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        ...
    }

3-2 ios 세팅

  • 추가 예정
profile
프론트엔드

0개의 댓글