Support Topics Documentation Slack YouTube Blog

Flutter Crash on register for Push Notifications

Backendless version: 6.4.0
Client SDK: Flutter
Backendless SDK: ^7.0.0-nullsafety.0

Dependencies
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  backendless_sdk: ^7.0.0-nullsafety.0
  firebase_core: ^1.1.1
  firebase_messaging: ^10.0.0
  flutter_native_splash: ^1.1.8+4
  google_fonts: ^2.0.0
  image_picker: ^0.7.4
  intl: ^0.17.0
  provider: ^5.0.0
  quiver: ^3.0.1
  flutter_flip_view: ^1.0.3

Expected Behavior

I was following the Missions to register Push Notifications for an Android device using Flutter. Attempting to do so crashes the app completely.

Actual Behavior

App crashes before launch.

Runtime Error Stack
E/AndroidRuntime(13726): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;
E/AndroidRuntime(13726): 	at com.backendless.push.FCMRegistration$1.onComplete(FCMRegistration.java:51)
E/AndroidRuntime(13726): 	at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
E/AndroidRuntime(13726): 	at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime(13726): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13726): 	at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime(13726): 	at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime(13726): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(13726): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime(13726): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/AndroidRuntime(13726): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" on path: DexPathList[[zip file "/data/app/~~Sm7Uki4ruFLS2wGN2e-iow==/com.widgetlib.dicedating-_x52LKM7Mf8EMZhl1WVtTA==/base.apk"],nativeLibraryDirectories=[/data/app/~~Sm7Uki4ruFLS2wGN2e-iow==/com.widgetlib.dicedating-_x52LKM7Mf8EMZhl1WVtTA==/lib/x86, /data/app/~~Sm7Uki4ruFLS2wGN2e-iow==/com.widgetlib.dicedating-_x52LKM7Mf8EMZhl1WVtTA==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
E/AndroidRuntime(13726): 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
E/AndroidRuntime(13726): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E/AndroidRuntime(13726): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E/AndroidRuntime(13726): 	... 9 more

Reproducible Test Case

An app with the following code and the same dependencies crashes on startup.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();

  await BackendlessManager.registerForPushNotifications(); // crashes the app
  
  Backendless.setUrl("https://eu-api.backendless.com");
  await Backendless.initApp(BackendlessManager.APPLICATION_ID,
      BackendlessManager.APPLICATION_ID, BackendlessManager.APPLICATION_ID);

  runApp(MyApp());
}

Thank you :slight_smile:

Hello @Maxime_Franchot,

I’ve created an internal ticket BKNDLSS-25175. Our Flutter developer will investigate this issue as soon as possible.

Regards,
Olha

Hi @Maxime_Franchot

Can you try to initialize the Backendless app before registering push notifications? Like this:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  Backendless.setUrl("https://eu-api.backendless.com");
  await Backendless.initApp(BackendlessManager.APPLICATION_ID,
      BackendlessManager.APPLICATION_ID, BackendlessManager.APPLICATION_ID);

  await Firebase.initializeApp();

  await BackendlessManager.registerForPushNotifications(); // crashes the app
  
  runApp(MyApp());
}

If the error still persists, please provide the stacktrace.

Best Regards,
Maksym

Thanks for your response.

My bad, I provided a version that I was messing around with to get working. I had in fact tried with the backendless app initialized first. Here’s the stacktrace that comes up when I run it in the right order:

Code
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  Backendless.setUrl("https://eu-api.backendless.com");
  await Backendless.initApp(BackendlessManager.APPLICATION_ID,
      BackendlessManager.APPLICATION_ID, BackendlessManager.APPLICATION_ID);

  await Firebase.initializeApp();

  await BackendlessManager.registerForPushNotifications(); // crashes the app

  runApp(MyApp());
}
Stack Trace
W/tlib.dicedatin( 6048): Accessing hidden method Landroid/app/ActivityThread;->currentApplication()Landroid/app/Application; (greylist, reflection, allowed)
E/AndroidRuntime( 6048): FATAL EXCEPTION: pool-BackendlessSDK-thread-0
E/AndroidRuntime( 6048): Process: com.widgetlib.dicedating, PID: 6048
E/AndroidRuntime( 6048): java.lang.ExceptionInInitializerError
E/AndroidRuntime( 6048): at com.backendless.HeadersManager.getInstance(HeadersManager.java:38)
E/AndroidRuntime( 6048): at com.backendless.Invoker.invokeSync(Invoker.java:95)
E/AndroidRuntime( 6048): at com.backendless.Invoker$1.run(Invoker.java:71)
E/AndroidRuntime( 6048): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 6048): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 6048): at java.lang.Thread.run(Thread.java:923)
E/AndroidRuntime( 6048): Caused by: java.lang.IllegalStateException: Backendless application was not initialized
E/AndroidRuntime( 6048): at com.backendless.AndroidBackendlessPrefs.getAuthKeys(AndroidBackendlessPrefs.java:119)
E/AndroidRuntime( 6048): at com.backendless.AndroidBackendlessPrefs.getApplicationId(AndroidBackendlessPrefs.java:50)
E/AndroidRuntime( 6048): at com.backendless.Backendless.getApplicationId(Backendless.java:277)
E/AndroidRuntime( 6048): at com.backendless.AndroidHeadersManager.<init>(AndroidHeadersManager.java:34)
E/AndroidRuntime( 6048): at com.backendless.AndroidHeadersManager.getInstance(AndroidHeadersManager.java:22)
E/AndroidRuntime( 6048): at com.backendless.HeadersManager.<clinit>(HeadersManager.java:28)
E/AndroidRuntime( 6048): ... 6 more

It still seems to be an issue with the application ID. I’ve double-checked that the Application/Android/iOS values I’ve inputted are correct - but the other issue I have, not being able to read from the database, may be caused by the same issue.

Edit:

I fixed the previous issue, which was cause by an await missing before setting the EU region (therefore the app not connecting properly). However, I’m still getting the issue registering firebase FCM tokens - I’ll look into it further on the firebase side of things but the error is coming from backendless:

Stack Trace
E/AndroidRuntime( 9793): FATAL EXCEPTION: main
E/AndroidRuntime( 9793): Process: com.widgetlib.dicedating, PID: 9793
E/AndroidRuntime( 9793): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/FirebaseInstanceId;
E/AndroidRuntime( 9793): at com.backendless.push.FCMRegistration$1.onComplete(FCMRegistration.java:51)
E/AndroidRuntime( 9793): at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
E/AndroidRuntime( 9793): at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime( 9793): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 9793): at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime( 9793): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime( 9793): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 9793): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime( 9793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/AndroidRuntime( 9793): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.FirebaseInstanceId" on path: DexPathList[[zip file "/data/app/~~GJpY-ue23YwXTWAI2L24IA==/com.widgetlib.dicedating-AX0HFmgF9skCp31oNjHZ5g==/base.apk"],nativeLibraryDirectories=[/data/app/~~GJpY-ue23YwXTWAI2L24IA==/com.widgetlib.dicedating-AX0HFmgF9skCp31oNjHZ5g==/lib/x86, /data/app/~~GJpY-ue23YwXTWAI2L24IA==/com.widgetlib.dicedating-AX0HFmgF9skCp31oNjHZ5g==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
E/AndroidRuntime( 9793): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
E/AndroidRuntime( 9793): at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E/AndroidRuntime( 9793): at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E/AndroidRuntime( 9793): ... 9 more

The error comes up both for Backendless SDK 6.3.2 and 7.0.0.

Edit no. 2:

Really sorry, notice the “Android API key” and “iOS API key” arguments are just being given the App ID. This being fixed, I’m able to communicate with the backend (the other issue is solved), but I’m still getting a crash on registering push notifications.

Push notifications from firebase messaging work, but as soon as I add the “register push notifications”, the crash occurs.

Edit no. 3:

Registration code
static Future registerForPushNotifications() async {

    if (await Backendless.isInitialized()) {
      await Backendless.messaging.registerDevice(["default"]);
    }

}

Details:

android/build.gradle
    buildscript {
        ext.kotlin_version = '1.3.50'

        ext {
            firebaseMessagingVersion = "21.0.0"
        }

        repositories {
            google()
            jcenter()
        }

        dependencies {
            classpath 'com.google.gms:google-services:4.3.5'
            classpath 'com.android.tools.build:gradle:4.1.0'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }

    allprojects {
        repositories {
            google()
            jcenter()
        }
        tasks.withType(JavaCompile) { 
            options.deprecation = true
        }
    }

    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
    }
    subprojects {
        project.evaluationDependsOn(':app')
    }

    task clean(type: Delete) {
        delete rootProject.buildDir
    }
android/app/build.gradle
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 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.widgetlib.dicedating"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    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
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
    implementation 'com.google.firebase:firebase-messaging:22.0.0'
    implementation 'com.android.support:multidex:1.0.3'
}
andoird/app/src/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.widgetlib.dicedating">
   <application
        android:label="dice_dating"
        android:icon="@mipmap/ic_launcher">
        <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">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <service android:name="com.backendless.push.BackendlessFCMService">
          <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
          </intent-filter>
        </service>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

Hi @Maxime_Franchot

Please make sure you have added socket.io dependency. It’s a required step described in the documentation:
https://backendless.com/docs/flutter/setup.html

Real-Time Dependency. If you plan on using the real-time database and/or messaging in your application, add the following dependency in android/app/build.gradle:

implementation (‘io.socket:socket.io-client:1.0.0’) {
// excluding org.json which is provided by Android
exclude group: ‘org.json’, module: ‘json’
}

Best Regards,
Maksym