Closed firatcetiner closed 3 years ago
I don't see this file in documentation which you have used - FirebaseCloudMessagingPluginRegistrant.kt . The documentation mentions MainActivity.kt and Appliaction.kt .
Code for MainActivity.kt -
package com.example.yourpackagename
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}
Code for Application.kt -
package com.example.yourpackagename
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
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
Apart from this, you also need to do some changes in the AndroidManifest.xml and app level build.gradle. Assuming that you have done those changes already, I would suggest you to try these changes given above.
Also I would like to know what do you want to do in the background message handler, because even though the background message handler is getting fired consistently in my application but its of no use because I am unable to do anything inside it except from printing some thing in the console.
Seems that the background message handler runs as an isolate - #3520 , I wanted to use Firestore in background but it isn't possible it seems, because FirebaseApp needs to be initialized which is itself not possible in an isolate.
Therefore, I tried to add what I wanted to do in sharedPreferences, so that I could perform those tasks when the app starts in the foreground, but unfortunately, I am unable to access sharedPreferences in the background message handler as well.
Seems like the backgroundMessageHandler is useless for now..
Hi @firatcetiner As mentioned above you need to configure background code properly on the Android side Nevertheless, I tried to reproduce data only payload (Excluding notification on Android)
DATA='{"priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "id": "1", "status": "done"}, "to": "dPB1w6TvRnid4pErhW-WCH:APA91bHwrUwt4GqZ13vfpUiz04rRpJuNaTLSYj_a8PvpES1oCg3VWvPOPPTjolIl-A-ZZt95lInRvaygnsCgUEDRsdQv27WV4IswVKawqqABrijS2hum8M1LUncJXnwrFIs54pO7-lEe"}'
curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key=AAAAcVC6H8o:APA91bF9eBcw6f4RbqJ89uYUGJ7RpMm2w0-yU8T8TIJ5LgXbjc91z_WxlOOzsUotZc6kYpqIQs7EnO5Im00xrLNuLpbkZ7VHgtFQ_Jv19W-XMi2dvWLteOZDTo77u8SzPHy-qF1bxBgp"
logs when each payload is sent
V/ActivityThread(16954): scheduleReceiver info = ActivityInfo{e28de0e com.google.firebase.iid.FirebaseInstanceIdReceiver} intent = Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x11080010 pkg=com.nevercode.triage cmp=com.nevercode.triage/com.google.firebase.iid.FirebaseInstanceIdReceiver (has extras) } sync = true hasCode = 210433762
D/ColorExSystemServiceHelper(16954): checkColorExSystemService className = com.google.android.gms.gcm.PushMessagingRegistrarProxy
V/ActivityThread(16954): scheduleReceiver info = ActivityInfo{9d16fc5 com.google.firebase.iid.FirebaseInstanceIdReceiver} intent = Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x11080010 pkg=com.nevercode.triage cmp=com.nevercode.triage/com.google.firebase.iid.FirebaseInstanceIdReceiver (has extras) } sync = true hasCode = 243983015
D/ColorExSystemServiceHelper(16954): checkColorExSystemService className = com.google.android.gms.gcm.PushMessagingRegistrarProxy
As mentioned above you need to configure background code properly on the Android side
@TahaTesser FirebaseCloudMessagingPluginRegistrant.ks already do that. What's the difference between my code and the one above?
Update: I changed my code to match the code above but the same issue appears even in the foreground message handler method, I can't reach any payload. This is very strange.
There is a 8.0 dev release out with major reworks to background handling if you want to give the release a try.
@Ehesp Thanks for pointing that out. By 8.0 dev I'm assuming you are referring firebase_messaging (dev) because I already tried that and nothing change.
Dependency
firebase_messaging:
git:
url: git://github.com/FirebaseExtended/flutterfire
ref: dev
path: ./packages/firebase_messaging
@firatcetiner is this issue happening on emulator or physical device? If it's happening on a physical device, which device you are using and have you tried another device?
@mdalihusain All tests done on physical devices. I have used Xiaomi Redmi Note 8 Pro (Android 10), Xiaomi Mi 6 (Android 9). Why should I worry about the manufacturer?
@mdalihusain All tests done on physical devices. I have used Xiaomi Redmi Note 8 Pro (Android 10), Xiaomi Mi 6 (Android 9). Should I worry about the manufacturer of device?
It's fine, mine is also Xiaomi Redmi Note 8 Pro (Android 10), and everything is working absolutely fine I asked about the device because I have read on forums that OnePlus strictly forces to stop background processes..
Only two other reasons I can think because of which the issue is occurring -
@mdalihusain Thanks for your help. Here are the files:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:name=".Application"
android:icon="@mipmap/ic_launcher"
tools:replace="android:label"
android:allowBackup="false"
android:usesCleartextTraffic="true"
android:fullBackupContent="false"
tools:targetApi="m">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<meta-data
android:name="flutterEmbedding"
android:value="2"/>
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true"/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />
</application>
</manifest>
build.gradle (module)
def 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: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 29
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "com.exampe.myapplication"
minSdkVersion 18
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
compileOptions {
sourceCompatibility "1.8"
targetCompatibility "1.8"
}
buildToolsVersion = '29.0.2'
}
flutter {
source '../..'
}
dependencies {
implementation 'com.google.firebase:firebase-messaging:20.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
implementation 'com.android.support:multidex:1.0.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
builld.gradle (project)
buildscript {
ext.kotlin_version = '1.4.10'
ext {
compileSdkVersion = 29 // or higher
targetSdkVersion = 29 // or higher
appCompatVersion = "1.0.2" // or higher
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.3'
classpath 'com.android.tools.build:gradle:3.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "${project(':background_fetch').projectDir}/libs"
}
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
What's the point of trying this plugin on a new project? I mean we always use other packages/plugins in our projects and it is almost impossible to determine which plugin cause incompatibilities with Firebase Messaging, or vice versa.
The build.gradle and AndroidManifest files seem to be alright...
The point with creating a new application is to make sure that another plugin is causing this issue or the problem is with Firebase messaging itself .
Well if that is the case it means I have to manually check every other plugin separately to see the incompatibilities, which is not ideal for me since I'm using tons of different packages/plugins. Anyway, I will try creating a new project using only Firebase Messaging.
@Ehesp Thanks for pointing that out. By 8.0 dev I'm assuming you are referring firebase_messaging (dev) because I already tried that and nothing change.
Dependency
firebase_messaging: git: url: git://github.com/FirebaseExtended/flutterfire ref: dev path: ./packages/firebase_messaging
We meant 8.0.0-dev.4
- see the migration guide: https://firebase.flutter.dev/docs/migration
I confirm that firebase_messaging 8.0.0-dev.4 resolves this issue and I'm going to close it, it would be good to have some ideas about when this new rework land on the stable channel. Thank you all for your help
I confirm that firebase_messaging 8.0.0-dev.4 resolves this issue and I'm going to close it, it would be good to have some ideas about when this new rework land on the stable channel. Thank you all for your help
@firatcetiner 1-2 weeks - providing no major issues reported whilst on dev channel.
What happened?
Firebase Cloud Messaging plugin's background message handler doesn't always work correctly. The message raised from Flutter debug console is
W/FirebaseInstanceId(17720): Service took too long to process intent: com.google.android.c2dm.intent.RECEIVE App may get closed.
. I get this "error" most of the times. See related issue #2181 and pull request #1774 (which claims to solve this issue). I was doing some sqlite operations with moor_flutter on that method and I completely ignored them for now. Right now the handler method is like this:And it doesn't event print 'message received'.
Steps to reproduce
Steps to reproduce the behavior:
firebase_messaging: 7.0.3
,Plugin Registry
Application.kt
FirebaseCloudMessagingPluginRegistrant.kt
flutter doctor -v
Flutter dependencies