Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter firebase_message plugin setup errors

I wanted to try out cloud messaging in my Flutter application, but I always run into errors, and I have not found a solution yet. I followed the steps stated here: firebase_messaging

If I follow the steps except the optional part with creating an Application.java file for background messages, and I send a test message from Firebase to the launched application, I get this error:

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
   at android.app.ActivityThread.access$1400(ActivityThread.java:200)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:201)
   at android.app.ActivityThread.main(ActivityThread.java:6806)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/AndroidRuntime(29468): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)

For me it seems like that the optional part is needed too, but when I do that I get errors when the app opens. After the errors the app still runs, and if I send a test message from Firebase the app receives it succesfully. Later it doesn't even receives the message, but thows the same errors.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference
   at com.github.adee42.keyboardvisibility.KeyboardVisibilityPlugin.registerWith(KeyboardVisibilityPlugin.java:107)
   at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:22)
   at io.flutter.plugins.Application.registerWith(Application.java:18)
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
   at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:133)
   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
   at android.os.MessageQueue.nativePollOnce(Native Method)
   at android.os.MessageQueue.next(MessageQueue.java:326)
   at android.os.Looper.loop(Looper.java:165)
   at android.app.ActivityThread.main(ActivityThread.java:6806)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference, null)
    StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
    MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
    <asynchronous suspension>
    FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
    main (package:vonatles/main.dart:18:22)
    _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
    main (package:vonatles/main.dart:12:10)
    _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:229:25)
    _rootRun (dart:async/zone.dart:1124:13)
    _CustomZone.run (dart:async/zone.dart:1021:19)
    _runZoned (dart:async/zone.dart:1516:10)
    runZoned (dart:async/zone.dart:1500:12)
    _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:221:5)
    _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:19)
    _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

Versions:

firebase_core: ^0.4.0+1  
firebase_database: 3.0.7  
firebase_messaging: 5.1.5 

classpath 'com.android.tools.build:gradle:3.3.0'  
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0"  
classpath 'com.google.gms:google-services:4.3.0'  

In the Flutter main function I have this:

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);

The Application.java code:

package io.flutter.plugins;

 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;

 public class Application extends FlutterApplication implements PluginRegistrantCallback {
   @Override
   public void onCreate() {
     super.onCreate();
     FlutterFirebaseMessagingService.setPluginRegistrant(this);
   }

   @Override
   public void registerWith(PluginRegistry registry) {
     GeneratedPluginRegistrant.registerWith(registry);
   }
 }

My android main directory:

-main  
  -java  
    -io.flutter.plugins  
      Apllication.java  
      GeneratedPluginRegistrant.java  
  -kotlin  
    -my.package.name  
      MainActivity.kt

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package.name">
    <application
        android:name="io.flutter.plugins.Application"
        android:label="mylabel"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            ...
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>
like image 481
B.G. Avatar asked Sep 11 '19 14:09

B.G.


1 Answers

Since my MainActivity in android was a Kotlin class, I got the solution to this problem when I tried the Application class as a Kotlin class instead of trying it as a Java class. I tried many solutions, still, I was not able to run the app.

Application.kt

package YOUR_PACKAGE_NAME

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

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

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

Also, 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"))
        }

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

This can be added in the sample directory where your MainActivity is.

Try this Along with the steps mentioned here: Firebase messaging

like image 196
devu mani Avatar answered Nov 16 '22 22:11

devu mani