-
Notifications
You must be signed in to change notification settings - Fork 4k
firebase-messaging: Android Integration #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey srsandy, you need to add the Application.java file in the same folder, where your MainActivity is. Then you have to modify that class according to your package name, which is:
So the first line of the file should be: Let me know if it worked :) |
My app also crashed. I'm already following every step. Please update firebase_messaging example for this background implementation. Thank you :) Logs
|
@markuspaschi no it did not work I still have |
Have you imported the FIrebaseMessaging library successfully via pubspec.yaml? Can you tell me if you have these lines in your GeneratedPluginRegistrant.java file, located under: android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
Then you should not have any problems resolving 🤔 |
I have this in my
now when i run the app.. this is what i get
|
You need a valid google-services.json file.
|
Thanks, @markuspaschi for all your help. Solved it. I don't was to close the issue I have a request from maintainers to update the docs. |
@srsandy I got the same problem "The method 'toRawHandle' was called on null" when using "onBackgroundMessage". About google-services.json and AndroidManifest.xml I put the same package name. So I'm really confused about the location of "Application.java". Where did you put that and what "package ...." line? Thanks in adv any helps!. |
@trieuvi your Application.java is correct. |
This happens, if the isolate is not found. You need an top-level function with the name "myBackgroundMessageHandler".
` |
@srsandy
In my pubspec.yaml:
I can't change name in pubspec.yalm because of Dart validation, do I have to change "project_id" as name in pubspec? @markuspaschi `Future myBackgroundMessageHandler(Map<String, dynamic> message) {
} void firebaseCloudMessaging_Listeners() {
}` Can you check any wrong here? |
@trieuvi keep if you will change the
|
@srsandy Sorry I don't understand this point:
My "package_name": "com.example.stn_app", same as package in AndroidManifest. |
this was the final prob with me when changes my name in |
How should I configure |
@OliverWangWei I wrote this in the MainActivity.kt file: class MainActivity: FlutterActivity(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(registry)
}
} |
@lynrin Do I need to import any code like the version of java and remove the original code in |
@OliverWangWei Sorry... It was a mistake to edit the MainActivity.kr file. I created a new Application.kr file and wrote it as follows: package io.flutter.plugins.firebasemessagingexample;
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(registry);
}
} Then proceed to Step 2 of Optionally handle background messages. When I rewritten AndroidManifest.xml to run it from newly added Application.kr, it worked. |
Was #111 (comment) resolved? For example, we have a project on firebase called "Digital Merchant" which makes the projectId = "digital-merchant", however in the pubspec the character "-" is not supported only "_" are supported so our pubspec will never match the google services file, please note we cannot just change the name of the project on firebase there is no option and our entire pipeline etc. is built around it, so I am hoping the answer is not to "simply change the name" as that is not a solution. |
I have a similar condition: my firebase project id is "pais-e-filhos" and it serves 2 apps: paiefilhopai and paiefilhofilho. All the environment is online and in production. Now, I need to implement FCM. How I can configure my apps to be served with only one Firebase project (with "-" in them name). I can´t rename none of them. Thanks. |
how you resolve it? |
Not solved iet... |
@htsnet have you taken a look at #199 (comment) ? After hours of searching, this complete comment helps me to successfully implement the new FCM version - I give you that the documentation needs to be deepen further. |
Hi @srsandy I'm closing the current one as duplicate. |
My app still crush on first time start any other solution please? |
i can't change my firebase project id |
for me decided by doing this: Aplication.ktpackage br.gov.rj.riodasostras.appsescverao
import android.os.Bundle
import io.flutter.app.FlutterActivity
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
//import com.google.firebase.messaging.FirebaseMessagingService
class Application : FlutterApplication() , PluginRegistrantCallback {
override fun onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith( registry: PluginRegistry) {
GeneratedPluginRegistrant.registerWith(registry);
}
} android/build.gradlebuildscript {
ext.kotlin_version = '1.3.0' //1.2.71
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1' //3.2.1 //3.5.3
classpath 'com.google.gms:google-services:4.3.3' //4.3.2
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
} android/app/build.gradledef localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
//inicio alteração
apply plugin: 'com.google.gms.google-services'
//fim
//importa arquivo de configuração da chave de assinatura do App
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
compileSdkVersion 28
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "br.gov.rj.riodasostras.appsescverao"
minSdkVersion 21
multiDexEnabled true
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
//sign release
signingConfigs {
release {
keyAlias localProperties['keyAlias']
keyPassword localProperties['keyPassword']
storeFile file(localProperties['storeFile'])
storePassword localProperties['storePassword']
}
}
buildTypes {
release {
// Signing with the debug keys for now, so `flutter run --release` works.
//signingConfig signingConfigs.debug
//abilita a versão assinada para publicação na Play store
signingConfig signingConfigs.release
//abilita obfuscação e mimificação
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
flutter {
source '../..'
}
dependencies {
//inicio alteração para abilita multidex e firebase
implementation 'com.android.support:multidex:2.0.0' //1.0.3
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation "com.google.firebase:firebase-analytics:17.2.1"
implementation "com.google.firebase:firebase-messaging:20.1.0"
//fim
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
//inicio alteração
apply plugin: 'com.google.gms.google-services'
//fim pubspec.yaml name: appsescverao
description: Aplicativo do Sesc Verão Rio das Ostras
version: 2.0.0+2
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
cupertino_icons: ^0.1.3
http: ^0.12.0+2
path_provider: ^1.5.1
flutter_advanced_networkimage: ^0.6.2
sqflite: ^1.2.0
connectivity: ^0.4.6+1
url_launcher: ^5.4.1
cloud_firestore: ^0.13.0+1
firebase_auth: ^0.15.2
google_sign_in: ^4.1.0
firebase_database: ^3.1.1
rxdart: ^0.23.1
webview_flutter: ^0.3.18+1
auto_size_text: ^2.1.0
flutter_map: ^0.8.0
latlong: ^0.6.1
queries: ^0.1.12
firebase_analytics: ^5.0.9
shared_preferences: ^0.5.6
share: ^0.6.3+5
map_launcher: ^0.3.2
#audioplayers: ^0.13.5
#audio_streams: ^1.0.0+2
firebase_messaging: ^6.0.9
dev_dependencies:
flutter_test:
sdk: flutter
intl_translation: ^0.17.7
flutter:
uses-material-design: true
assets:
- assets/logo.png
- assets/background.png
- assets/background2.png
- assets/background3.png
- assets/user-avatar.png
- assets/logo-pmro-cinza.png
- assets/logo-pmro-branco.png
- assets/wave-bottom.png
- assets/wave-top.png
- assets/logo2.png
- assets/logo-branca.png
fonts:
- family: Big Noodle Titling
fonts:
- asset: fonts/big_noodle_titling.ttf
- family: GlosaDisplayW01-Bold
fonts:
- asset: fonts/glosa_display_w01_bold.ttf
- family: BakerieSmoothCondensedMedium
fonts:
- asset: fonts/BakerieSmoothCondensed-Medium.otf
- family: BakerieSmoothBold
fonts:
- asset: fonts/BakerieSmooth-Bold.otf
- family: iconsmind
fonts:
- asset: fonts/iconsmind.ttf |
don't forget to remove
from the mainactivity file under the android folder. If not, you will get an error. |
Could everyone who still has this problem please file a new issue with the exact descriptions what happens, logs and the output of 'flutter doctor -v' please. |
I had similar issue and I had to write Application.kt like this: Note: After this reinstall app on mobile device
|
@fehimdervisbegovic this seem the only it worked with me as well. I don't know why the docs don't have an example on how to setup it properly for Kotlin projects. |
@fehimdervisbegovic does this require the |
@fehimdervisbegovic or @miguelpruivo, do you have an working example you could share?
|
@insinfo Can you add AndroidManifest.xml too? |
@akilawickey thanks for sharing. |
I have error: |
Did you ever upload that repo? |
\android\app\src\main\kotlin\Application.kt
\android\app\src\main\kotlin\FirebaseCloudMessagingPluginRegistrant.kt
\pubspec.yaml
\android\build.gradle
\android\app\build.gradle
|
Android Integration
I'm not an android developer. I'm trying to use FCM with flutter
Using
firebase_messaging: ^5.1.5
In android integrations, there is a section
Optionally handle background messages
Step 1 says Add an Application.java class to your app
I'm confused wherein the app.
I tried adding it to all the places but my application crashes. No console errors but not able to run the application.
I have this exact code in my
Application.java
also I have updated
AndroidManifest.xml
Also, I get error's like
firebasemessaging
&firebasemessagingexample
not resolved.Improvements
If possible pls explicitly method where should be added.
Thank You in adv!.
The text was updated successfully, but these errors were encountered: