flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[firebase_messaging] PluginRegistrantCallback is not set on Android

Open creativecreatorormaybenot opened this issue 6 years ago • 85 comments

Fatal Exception: java.lang.RuntimeException
Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: 
java.lang.RuntimeException: PluginRegistrantCallback is not set.

This is caused by:

Caused by java.lang.RuntimeException: PluginRegistrantCallback is not set.
       at io.flutter.plugins.firebasemessaging.
FlutterFirebaseMessagingService.executeDartCallbackInBackgroundIsolate(FlutterFirebaseMessagingService.java)
       at io.flutter.plugins.firebasemessaging.
FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java)

This happens on Android when running a Flutter app built with firebase_messaging: 6.0.9 and Flutter 1.15.4-pre.199.


This also happens every time calling FirebaseMessaging.configure in the Flutter app when running on Android:

PlatformException(error, PluginRegistrantCallback is not set., null)
  StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
  MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
  MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
  FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:130:16)

exact same issue. the error appears when using background notification.

``` `E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): Failed to handle method call E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): java.lang.RuntimeException: PluginRegistrantCallback is not set. E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:176) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at android.os.MessageQueue.next(MessageQueue.java:326) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at android.os.Looper.loop(Looper.java:160) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at android.app.ActivityThread.main(ActivityThread.java:6669) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E/MethodChannel#plugins.flutter.io/firebase_messaging(12720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I/mattuvandi.use(12720): Background concurrent copying GC freed 18711(1011KB) AllocSpace objects, 12(304KB) LOS objects, 50% free, 2MB/4MB, paused 11.526ms total 155.904ms

E/flutter (12720): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null) E/flutter (12720): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7) E/flutter (12720): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33) E/flutter (12720): E/flutter (12720): #2 FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:130:16) E/flutter (12720): #3 firebaseNotification (package:Mattuvandi/app_screens/home/Home.dart:432:27) E/flutter (12720): #4 _HomeState.build (package:Mattuvandi/app_screens/home/Home.dart:48:7) E/flutter (12720): #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27) E/flutter (12720): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15) E/flutter (12720): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (12720): #8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (12720): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (12720): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (12720): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (12720): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (12720): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (12720): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (12720): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (12720): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (12720): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (12720): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (12720): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (12720): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (12720): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (12720): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (12720): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (12720): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (12720): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (12720): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (12720): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (12720): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (12720): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (12720): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (12720): #45 Com`

</details>

ClevercodeTech avatar Mar 03 '20 16:03 ClevercodeTech

same issue, is there any solution yet?

azabazay avatar Mar 10 '20 12:03 azabazay

Application.java replace:
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);
  }
}
Application.java with:
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 io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {

    @Override
    @CallSuper
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry pluginRegistry) {
        FirebaseMessagingPlugin.registerWith(pluginRegistry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

h0x539 avatar Mar 15 '20 18:03 h0x539

I run in to error while using @callsuper. As per android document it is deprecated. with your suggested code i get same error.

manifest.xml:

`
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
     calls FlutterMain.startInitialization(this); in its onCreate method.
     In most cases you can leave this as-is, but you if you want to provide
     additional functionality it is fine to subclass or reimplement
     FlutterApplication and put your custom class here. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />       
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="Mattuvandi"
    android:icon="@mipmap/ic_launcher">
 
     <meta-data android:name="com.google.android.geo.API_KEY"
           android:value="*************************************"/>               
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- This keeps the window background of the activity showing
             until Flutter renders its first frame. It can be removed if
             there is no splash screen (such as the default splash screen
             defined in @style/LaunchTheme). -->
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <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=".Application"
              android:launchMode="singleTop"
              android:theme="@android:style/Theme.Black.NoTitleBar"
              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
              android:hardwareAccelerated="true"
              android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>     
</application>   

`

Mainactivity.Java
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this); //auto generated?
  }
}

Application.java

```` package com.mattuvandi.user; import android.os.Bundle; import io.flutter.app.FlutterApplication; import io.flutter.app.FlutterActivity; 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 io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

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

@Override public void registerWith(PluginRegistry pluginRegistry) { FirebaseMessagingPlugin.registerWith(pluginRegistry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")); } }````

Error:

````E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): Failed to handle method call E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): java.lang.RuntimeException: PluginRegistrantCallback is not set. E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:176) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at android.os.MessageQueue.nativePollOnce(Native Method) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at android.os.MessageQueue.next(MessageQueue.java:326) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at android.os.Looper.loop(Looper.java:160) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at android.app.ActivityThread.main(ActivityThread.java:6669) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E/MethodChannel#plugins.flutter.io/firebase_messaging(14771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/flutter (14771): Login Status true I/mattuvandi.use(14771): Background concurrent copying GC freed 12428(1431KB) AllocSpace objects, 23(684KB) LOS objects, 49% free, 2024KB/3MB, paused 1.106ms total 237.579ms E/flutter (14771): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null) E/flutter (14771): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7) E/flutter (14771): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33) E/flutter (14771): E/flutter (14771): #2 FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:130:16) E/flutter (14771): #3 firebaseNotification (package:Mattuvandi/app_screens/home/Home.dart:432:27) E/flutter (14771): #4 _HomeState.build (package:Mattuvandi/app_screens/home/Home.dart:48:7) E/flutter (14771): #5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27) E/flutter (14771): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15) E/flutter (14771): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (14771): #8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (14771): #9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (14771): #10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (14771): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #13 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (14771): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #16 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (14771): #17 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (14771): #18 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (14771): #19 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (14771): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (14771): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #25 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (14771): #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #28 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (14771): #29 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (14771): #30 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (14771): #31 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) E/flutter (14771): #32 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5) E/flutter (14771): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (14771): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #38 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14) E/flutter (14771): #39 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14) E/flutter (14771): #40 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12) E/flutter (14771): #41 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16) E/flutter (14771): #42 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) E/flutter (14771): #43 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5) E/flutter (14771): #44 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11) ````

ClevercodeTech avatar Mar 16 '20 05:03 ClevercodeTech

@ClevercodeTech @h0x539 Could you maybe wrap your code into a <details> </details> to make this thread more readable?

@ClevercodeTech @h0x539 Could you maybe wrap your code into a <details> </details> to make this thread more readable?

thanks for the suggestion. Updated as per your suggestion.

ClevercodeTech avatar Mar 16 '20 08:03 ClevercodeTech

@ClevercodeTech

Try to make a standalone build of the android module.

Android Studio -> right click on android package -> Flutter -> Open Android module in Android Studio

Flutter Android module in Android Studio -> Terminal -> ./gradlew clean && ./gradlew build

h0x539 avatar Mar 27 '20 13:03 h0x539

Resolved the issue. Flutter instruction was not clear or i didn't get what they were saying. i changed the following line in the beginning of my manifest.xml:

~~android:name="io.flutter.app.FlutterApplication"~~

to

android:name=".Application"

<application ~~android:name="io.flutter.app.FlutterApplication"~~ android:name=".Application"
android:label="Mattuvandi" android:icon="@mipmap/ic_launcher">




ClevercodeTech avatar Mar 28 '20 17:03 ClevercodeTech

I did it and the error is out, but it is not calling the onBackgroundMessage anymore.

kalibu avatar Mar 31 '20 19:03 kalibu

Closing this issue because it looks like it's resolved.

@kalibu if you are still experiencing issues calling the onBackgroundMessage with the latest version of Flutter and FlutterFire plugin, can you please provide your updated flutter doctor -v and your flutter run --verbose/flutter build --verbose ? Also, to better address the issue, would be helpful if you could post a self contained app on github or the steps to reproduce it. Thank you

helenaford avatar Apr 20 '20 14:04 helenaford

@helenaford

This has not been resolved at all.
Hint: there has not been an update to the plugin (on Android) since I opened this issue. I only replaced one deprecated method that had nothing to do with this, so basically there has not been an update since I opened the issue..

@creativecreatorormaybenot did you try the solution provided by @ClevercodeTech ?

helenaford avatar Apr 20 '20 14:04 helenaford

@helenaford That "solution" only makes the error message go away and does not fix the underlying problem (the plugin is broken on Android, see #2017 for a start; there are way more issues for background messages).

The plugin would at least need to address this somewhere. Even if the underlying problem is not fixed immediately, the README section and more would need to be updated.

@helenaford That "solution" only makes the error message go away and does not fix the underlying problem (the plugin is broken on Android, see #2017 for a start; there are way more issues for background messages).

The plugin would at least need to address this somewhere. Even if the underlying problem is not fixed immediately, the README section and more would need to be updated.

Background message works for me.

you need to add a static method to capture the incoming message from firebase server. Below is a sample code

static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async { print(message); return Future<void>.value(); }

below is the detailed code

 import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/material.dart';
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

class FirebaseClass {

 firebaseNotification(context,topic ) {
   var platform = Theme.of(context).platform;
   final requestServer = RequestServer();
   print("firebaseinitiated");
   _firebaseMessaging.subscribeToTopic(topic);
   _firebaseMessaging.configure(
     onMessage: (Map<String, dynamic> message) async {
       print("onMessage: $message");
     },
     onBackgroundMessage: platform == TargetPlatform.iOS ? null : myBackgroundMessageHandler,
     onLaunch: (Map<String, dynamic> message) async {
       print("onLaunch: $message");
     },
     onResume: (Map<String, dynamic> message) async {
       print("onResume: $message");
     },
   );
   if (platform == TargetPlatform.iOS) {
     _firebaseMessaging
         .requestNotificationPermissions(const IosNotificationSettings(sound: true, badge: true, alert: true));
     _firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
       print("Settings registered: $settings");
     });
   }
   _firebaseMessaging.getToken().then((String token) {
     assert(token != null);
  //setting fire base toke in own server this is needed to message from your server
// requestServer is a custom class to connect with my server
     requestServer.requestServer("SetFirebaseToken", context).then((e) {
       e == 1 ? print("Push Messaging token: $token") : print("errror sending data to server");
     });
   });

 }

 notification() async {
   FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
   var initializationSettingsAndroid = new AndroidInitializationSettings('logo');
   var initializationSettingsIOS = new IOSInitializationSettings();
   var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
   flutterLocalNotificationsPlugin.initialize(
     initializationSettings, 
   );
   var androidPlatformChannelSpecifics = AndroidNotificationDetails(
       'your channel id', 'your channel name', 'your channel description',
       importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
   var iOSPlatformChannelSpecifics = IOSNotificationDetails();
   var platformChannelSpecifics = NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
   await flutterLocalNotificationsPlugin.show(0, 'plain title', 'plain body', platformChannelSpecifics,
       payload: 'item x');
 }

 static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
   print(message);
   return Future<void>.value();
 }

}

ClevercodeTech avatar Apr 20 '20 17:04 ClevercodeTech

@ClevercodeTech

If that works with all versions (Flutter beta + master too), then I suppose that would be a solution.
Still, the issue should remain open until this is at least integrated into the README.

@ClevercodeTech Can you provide detailed instructions?

demon-zhonglin avatar Apr 24 '20 06:04 demon-zhonglin

@ClevercodeTech Can you provide detailed instructions?

Please follow the instruction as shown in the following link https://pub.dev/packages/firebase_messaging

for receiving background message. importantly you need to change your android manifest.

usually flutter application starts like this

<application
    android:name="io.flutter.app.FlutterApplication"

you need to change the name to below

<application android:name=".Application" ...>

.Application refers to Application.java. Which contains below. package com.mattuvandi.user is my application. This should be changed to yours. e.g

package com.domain.application

package com.mattuvandi.user; 
import android.os.Bundle; 
import io.flutter.app.FlutterApplication; 
import io.flutter.app.FlutterActivity;
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 io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

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

@override
public void registerWith(PluginRegistry pluginRegistry) {
FirebaseMessagingPlugin.registerWith(pluginRegistry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}

ClevercodeTech avatar Apr 24 '20 12:04 ClevercodeTech

Doesn't work for me. Tried to switch branch from stable to master, didn't help.

lexdevel avatar Apr 27 '20 12:04 lexdevel

Doesn't work for me. Tried to switch branch from stable to master, didn't help.

could you please post your code.

ClevercodeTech avatar Apr 29 '20 07:04 ClevercodeTech

Sure.

// Application.kt

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

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
        // ...
    }
}
// AndroidManifest.xml

<application
    android:name=".Application" // Changed from `io.flutter.app.FlutterApplication`
    ... />
// build.gradle under app

// ...

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    implementation 'com.google.firebase:firebase-messaging:20.1.6'

    // ...
}

lexdevel avatar Apr 29 '20 07:04 lexdevel