고래 정보 분류소

[Flutter] firebase_message background handling 본문

Flutter

[Flutter] firebase_message background handling

블루웨일 2020. 3. 12. 23:43

Android Gradle 환경

 

buid.gradle (Project) :

 

dependencies {

    classpath 'com.android.tools.build:gradle:3.5.3'

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    classpath 'com.google.gms:google-services:4.3.2'  // Google Services plugin

}

 

build.gradle (App) :

 

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:runner:1.1.1'

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'com.google.firebase:firebase-messaging:20.1.2'

}



apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

 

기본 설치 및 설정은 아래 링크 참조

https://pub.dev/packages/firebase_messaging

 

firebase_messaging | Flutter Package

Flutter plugin for Firebase Cloud Messaging, a cross-platform messaging solution that lets you reliably deliver messages on Android and iOS.

pub.dev

 

Background Handling은 위 사이트대로 하면 Android의 Application 코드에서 오류가난다.

아래 코드대로 코딩

 

By Kotlin

 

MainActivity.kt

package Your.Package.Name

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

 

Application.kt

package Your.Package.Name


import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry!!)
    }
}

 

FirebaseCloudMessagingPluginRegistrant.kt

package Your.Package.Name

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

class FirebaseCloudMessagingPluginRegistrant {
    companion object {
        fun registerWith(registry: PluginRegistry) {
            if (alreadyRegisteredWith(registry)) {
                return
            }
            FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
        }

        private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
            val key = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName
            if (registry.hasPlugin(key)) {
                return true
            }
            registry.registrarFor(key)
            return false
        }
    }

}

 

위 부분만 수정하면 앱이 종료되어도 메시지 잘 받는다.

'Flutter' 카테고리의 다른 글

[Flutter] Plugin의 Background callback의 문제점  (0) 2020.03.17