Migration from GCM to FCM

Note

최신 샘플 및 라이브러리는 아래 링크를 통해, 다운로드 받을 수 있습니다.

1. 개요

Morpheus 플랫폼에서 제공하는 푸시 서비스 중, GCM을 FCM으로 변환하기 위한 문서이다.

2. 변환 순서 (Morpheus IDE 환경)

  • Morpheus Project 를 Gradle 환경으로 변환

  • Manifest.xml 수정

  • AndroidManifest.xml GCM 속성을 FCM 으로 변환

  • FCM console 에서 google-service.json 파일 다운로드 및 적용

  • build.gradle 설정

2.1. Morpheus Project 를 Gradle 환경으로 변환

가. 방법 1 (Android Studio 같이 사용)

나. 방법 2 (모피어스 IDE 기능 이용)

2.2 Manifest.xml 수정
<settings>
   <push>
       <receiver> <!-- UPMC 설정 정보 -->
           <!-- 브로드캐스트 리시버에서 퍼미션 사용 여부를 설정 (Y/N) android 8.0 이상 필수 -->
           <use-permission>Y</use-permission>
       </receiver>
   </push>
</settings>

2.3. AndroidManifest.xml 수정

가. GCM 속성 제거

 <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- for Gingerbread GSF backward compat -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="[패키지명]" />
        </intent-filter>
    </receiver>

    <service android:name="m.client.push.library.service.GCMIntentService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

    <service android:name="m.client.push.library.service.GCMInstanceIDListenerService" android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID"/>
        </intent-filter>
    </service>

<permission android:name="[패키지명].permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="[패키지명].permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
나. GCM 라이브러리 > FCM 라이브러리로 교체
  • gcm.jar or google-play-gcm.jar : 제거

  • mqtt*.jar : 교체

  • pushLibrary.jar : 교체

  • push_plugin*.jar : 유지

다. FCM 속성 추가

<service android:name="m.client.push.library.service.GCMIntentService" android:exported="false">
     <intent-filter>
         <action android:name="com.google.firebase.MESSAGING_EVENT" />
     </intent-filter>
 </service>

 <!-- Internal (not exported) receiver used by the app to start its own exported services
      without risk of being spoofed. -->


 <!-- FirebaseInstanceIdService performs security checks at runtime,
      no need for explicit permissions despite exported="true" -->
 <service android:name="m.client.push.library.service.GCMInstanceIDListenerService" android:exported="true">
     <intent-filter >
         <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
     </intent-filter>
 </service>

라. 사용자 퍼미션 추가

<!-- targetSDK 26 이상 필수 -->
<permission android:name="${applicationId}.permission.MPUSH_PERMISSION" android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.MPUSH_PERMISSION" />

2.4. GCM 프로젝트를 FCM 프로젝트로 가져오기

가. 이미 서비스 되고 있는 GCM의 환경(sender id / server key)를 FCM에서 유지합니다.

이유는, 이미 배포된 앱이 업그레이드 되기 전까지 동일하게 푸시 서비스를 받도록 유지해야 하기 때문입니다. 자세한 방법은 아래 링크의 Import your GCM project as a Firebase project 항목을 참고하시기 바랍니다.

2.5. FCM console 에서 google-service.json 파일 다운로드 및 적용

나. 적용 위치app/ 또는 module

모피어스 프로젝트의 경우, 프로젝트 root에 저장

2.6. Gradle Settings

  • dependencies 선언(적용 버전은 유동적임)

    implementation ('com.google.firebase:firebase-messaging:15.0.2')
    
  • apply plugin 선언(build.gradle 최하단에 위치 해야 함)

    apply plugin: 'com.google.gms.google-services'
    

2.7. build.gradle Sample [Morpheus IDE]

buildscript {
        repositories {
                jcenter()
                maven {url "https://maven.google.com"}
                maven {url "https://jcenter.bintray.com"}
        }

        dependencies {
                classpath 'com.android.tools.build:gradle:3.0.1'
                classpath 'com.google.gms:google-services:3.1.0'
                classpath 'com.github.ksoichiro:gradle-eclipse-aar-plugin:+'
        }
}

apply plugin: 'com.android.application'
apply plugin: 'com.github.ksoichiro.eclipse.aar'

repositories {
        jcenter()
        maven {url "https://maven.google.com"}
        maven {url "https://jcenter.bintray.com"}
}

android {
        compileSdkVersion 27
        buildToolsVersion "27.0.2"
        sourceSets {
                main {
                        manifest.srcFile 'AndroidManifest.xml'
                        java.srcDirs = ['src']
                        resources.srcDirs = ['src']
                        aidl.srcDirs = ['src']
                        renderscript.srcDirs = ['src']
                        res.srcDirs = ['res']
                        assets.srcDirs = ['assets']
                        jniLibs {
                                srcDir 'libs'
                        }
                }

                instrumentTest.setRoot('tests')
                debug.setRoot('build-types/debug')
                release.setRoot('build-types/release')
        }

        defaultConfig {
                multiDexEnabled true
        }

        dexOptions {
                preDexLibraries = false
        }

        lintOptions {
                checkReleaseBuilds false
                abortOnError false
        }

        buildTypes {
                release {
                        //minifyEnabled true
                        //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
                }
        }
}

dependencies {
        //구버전 중 빌드시 불 필요한 라이브러리는 exclude 처리 한다. [support-v4, gcm]
    implementation fileTree(dir: 'mcoreLibs', include: '*.jar', exclude: ['android-support-v4.jar', 'google-play-gcm.jar'])
    implementation 'com.android.support:appcompat-v7:23.0.1'

 // fcm sdk
    implementation ('com.google.firebase:firebase-messaging:15.0.2')
    implementation ('com.firebase:firebase-jobdispatcher:0.8.5')

}

apply plugin: 'com.google.gms.google-services'

2.8. build.gradle Sample [AndroidStudio]

  • Sample [Project]

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:3.1.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {

    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • Sample [Module]

apply plugin: 'com.android.application'
repositories {
    maven { url 'https://maven.google.com' }
}


android {
    compileSdkVersion 23
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "kr.co.pushdemo"
        minSdkVersion 14
        targetSdkVersion 26
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    implementation files('libs/*.jar')
    implementation ('com.google.firebase:firebase-messaging:15.0.2')
    implementation ('com.firebase:firebase-jobdispatcher:0.8.5')
}

apply plugin: 'com.google.gms.google-services'

2.9. Gradle Build시 주의 사항

Warning

아래와 같이 Error가 발생하는 경우, dependencies 속성이 Gradle 버전과 맞지 않기 때문이므로, implementation 을 compile 로 변경한다.

Error : A problem occurred evaluating project ‘:app’. > Could not find method implementation() for arguments