8

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>
8
  • have you added your Application class to AndroidManifest.xml ? if no then replace FlutterApplication with Applcation class you have created. Commented Sep 12, 2019 at 4:54
  • @HarishPenta I've added it to the manifest, but I'm not completely sure about that I did it correctly. I've appended the manifest to the post. Commented Sep 12, 2019 at 7:34
  • replace your Application class from io.flutter.plugins package into your kotlin my.package.name then change the path of Application Class in the AndroidManifest file, then let me know. Commented Sep 12, 2019 at 7:39
  • @HarishPenta I still get the NullPointerException error for Activity.getApplication(). But one more thing is, that if I set the onBackgroundMessage in the configuration as in the tutorial, it throws an error too ('NoSuchMethodError: The method 'toRawHandle' was called on null.') Commented Sep 12, 2019 at 15:37
  • did you set apply plugin: 'com.google.gms.google-services' in your build.gradle file? If so, then post a link to your app on GitHub & I'll take a look. To tell you what's wrong, I need to zoom out & see the entirety of your app - something's off with the "order of operations" somewhere. Commented Sep 16, 2019 at 21:51

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only one that worked for me after hours of trying, thanks for the great info and answer :)
0

by the way you can create a notification_handler.dart like this Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null. i have posted my notification_handler.dart their. try to configure with that and just call is as below from your statefullWidget and the let me know.

   @override
    void initState() {
      super.initState();
      new NotificationHandler().initializeFcmNotification();
    }

Comments

0

Background notification setup is not optional anymore. Make sure to follow intructions properly. And i was confused in making change in AndroidManifest.xml, So i wanted to share clear instruction. PluginRegistrantCallback is not set" means Application.java file is not called properly at AndroidManifest.xml. Make change as follows:

First it looks like this

Change it to yourpackagename.Application like this

If you get "Default activity not found" error, run "flutter clean" command in your project folder and run it again.

Comments

0

In your AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package.name">
<application
    android:name=".Application" //edit this line
    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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.