일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 플러터
- Flutter #Android #FCM #FirebaseCloudMessaging
- 안드로이드
- Permission
- 권한부여
- Android
- MySQL
- 사용자추가
- C++
- 사용자삭제
- password
- policy
- MariaDB
- thread
- background_callback
- 백그라운드
- 권한
- YouTube API
- C++ Debugging
- C++ Build
- 흰셔츠 #누런때
- Lockscreen
- User
- 잠금화면
- Flutter
- Background
- firebase_message
- VSCode
- 쓰레드
- Today
- Total
고래 정보 분류소
[Flutter] firebase_message background handling 본문
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
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 |
---|