Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when using in combination with firebase_messaging #45

Closed
Niek opened this issue Oct 3, 2019 · 7 comments · Fixed by #48
Closed

Crash when using in combination with firebase_messaging #45

Niek opened this issue Oct 3, 2019 · 7 comments · Fixed by #48
Labels
help wanted Extra attention is needed

Comments

@Niek
Copy link
Contributor

Niek commented Oct 3, 2019

When using firebase_messaging, you need to to add your own Application class that implements PluginRegistrantCallback. However, this causes WiFiFlutter to crash with the following trace:

E/MethodChannel#plugins.flutter.io/firebase_messaging(10125): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at com.alternadom.wifiiot.WifiIotPlugin.<init>(WifiIotPlugin.java:65)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at com.alternadom.wifiiot.WifiIotPlugin.registerWith(WifiIotPlugin.java:76)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:26)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at com.mypack.myapp.Application.registerWith(Application.java:18)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:134)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/firebase_messaging(10125):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

It's coming from this call: https://github.com/alternadom/WiFiFlutter/blob/a8bb37f87425e45d2947e6c7c4e8b9416c886364/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java#L65

@florian-guillemard
Copy link
Collaborator

florian-guillemard commented Oct 7, 2019

Hi,

Seems your poActivity is null, can you show me your call to your WifiIotPlugin constructor ?

@florian-guillemard florian-guillemard added the help wanted Extra attention is needed label Oct 7, 2019
@Niek
Copy link
Contributor Author

Niek commented Oct 7, 2019

The WifiIotPlugin contructor is called from registerWith():
https://github.com/alternadom/WiFiFlutter/blob/a8bb37f87425e45d2947e6c7c4e8b9416c886364/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java#L76

This is my Application.java, it is missing anything? Maybe add a call to onCreate()?

package com.my.app;

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

@daadu
Copy link
Member

daadu commented Nov 8, 2019

@Niek @alternadom Facing the same issue. Did you find any solution?

@daadu
Copy link
Member

daadu commented Nov 8, 2019

@daadu
Copy link
Member

daadu commented Nov 8, 2019

A similar problem occurred with android_alarm_manager and google_maps_flutter

This was the workaround they implemented for it: flutter/plugins#1255

In conclusion:

A plugin method channel can be called without activity. Hence not registering plugin when activity is null is the workaround [as used in the PR mentioned]

@daadu
Copy link
Member

daadu commented Nov 8, 2019

I confirm that this fixes the issue.

@Niek Please try and report

Put this in pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  firebase_core: 0.4.0+9
  firebase_messaging: ^5.1.6
  wifi_iot:
    git:
      #FIXME https://github.com/alternadom/WiFiFlutter/issues/45
      #url: "https://github.com/alternadom/WiFiFlutter.git"
      url: "https://github.com/daadu/WiFiFlutter.git"
      ref: "#45"

@Niek
Copy link
Contributor Author

Niek commented Nov 8, 2019

Thanks @daadu - I can confirm this fixes the issue! @alternadom can you merge the change?

florian-guillemard pushed a commit that referenced this issue Nov 8, 2019
making plugin foreground only
daadu added a commit that referenced this issue May 12, 2021
daadu pushed a commit that referenced this issue May 12, 2021
making plugin foreground only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants