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

Broken HowTo/Example... #2410

Closed
flutter2k20 opened this issue Apr 21, 2020 · 13 comments
Closed

Broken HowTo/Example... #2410

flutter2k20 opened this issue Apr 21, 2020 · 13 comments
Labels
plugin: messaging Stale Issue with no recent activity type: documentation Improvements or additions to documentation

Comments

@flutter2k20
Copy link

flutter2k20 commented Apr 21, 2020

Hi,

Can someone from the support team please fix the read me/example on pub.dev?

https://pub.dev/packages/firebase_messaging

I have followed the exact steps to add firebase messaging to a newly created project. When I do as suggested on the link above

  1. Add an Application.java class to your app in the same directory as your MainActivity.java. This is typically found in /android/app/src/main/java//.

and I am getting errors like:

error: cannot access FirebaseMessagingService
    FlutterFirebaseMessagingService.setPluginRegistrant(this);
                                   ^

The github example app is doing something completely different. Can someone please have a single version for howto use this plugin? Not sure if the example project is the new version or the one on pub.dev!

Thanks!

@Salakar Salakar added plugin: messaging type: documentation Improvements or additions to documentation labels Apr 21, 2020
@themisir
Copy link

Please someone who successfully installed fcm let us knw what we should do exactly :/

@flutter2k20
Copy link
Author

Can someone please bump up the priority of this issue?

The plugin is not usable for anyone trying to add it to their project, so this not only a documentation issue. For all practical purposes, anyone trying to integrate firebase_messaging into their project now, can't do it!

@lawkira2
Copy link

the same issue with me can anyone help

@a-elhaddad
Copy link

a-elhaddad commented Apr 22, 2020

Add this class :


public class FirebaseCloudMessagingPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

and call it in Application like

public class App extends FlutterApplication implements PluginRegistrantCallback {

    public void onCreate() {
        super.onCreate();
       // Stetho.initializeWithDefaults(this);
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this);
        NeverCrash.init(new NeverCrash.CrashHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                FileUtils.writeToFile(App.this, "bug " + System.currentTimeMillis(), Log.getStackTraceString(e));
            }
        });


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

dont forget to add name=".App" in the manifest file.

@themisir
Copy link

@a-elhaddad Thanks. Could you add imports to the code?

@flutter2k20
Copy link
Author

Add this class :


public class FirebaseCloudMessagingPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

and call it in Application like

public class App extends FlutterApplication implements PluginRegistrantCallback {

    public void onCreate() {
        super.onCreate();
       // Stetho.initializeWithDefaults(this);
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this);
        NeverCrash.init(new NeverCrash.CrashHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                FileUtils.writeToFile(App.this, "bug " + System.currentTimeMillis(), Log.getStackTraceString(e));
            }
        });


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

dont forget to add name=".App" in the manifest file.

Do you know if this java code is really required? If you are using flutter with v2 embedding, do need this code? or is it for the older version of flutter?

@themisir
Copy link

@a-elhaddad thanks, your code worked well. Here's kotlin translation:

FirebaseCloudMessagingPluginRegistrant.kt

import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.plugin.common.PluginRegistry

object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

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

Application.kt

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

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

    override fun registerWith(registry: PluginRegistry?) {
        if (registry != null) {
            FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
        }
    }
}

@IzioDev
Copy link

IzioDev commented Apr 23, 2020

I'm on the beta channel of flutter. I copied the kotlin files, set package on the top and edit the manifest to change MainActivity into Application but the application crash immediatly. It does build tho...

I ran flutter clean, tried to check my package path but. Not working, do you guys had a similar issues ?

@themisir
Copy link

@izio38 my solution works for master and stable branches. Did not tested on beta.

@joaovvrodrigues
Copy link

However, these proposed solutions are improvised, as it only allows the registration of 1 package, so it is not possible to use it together with WorkManager or AlarmManager, as the only package registered in the background is firebase. They need to update the tutorial on pub.dev, as they are upgrading to Android V2 but I did SEVERAL tests and it seemed like a very bad transition. It is not working, so it is necessary to improvise and be content with the use of only 1 package in the background. I hope they soon update the README - Getting Started from pub.dev to Android v2 embedding.

@vekexasia
Copy link

Wondering when this is going to be fixed. I Basically can't use the package now.

@renenucci
Copy link

Wondering when this is going to be fixed. I Basically can't use the package now.

Like me... its impossible to get runing background notifications... I mean, the push occurs and shows the notification, but the onBackgroundMessage never works...

@Salakar Salakar added the Stale Issue with no recent activity label Apr 1, 2021
@Salakar
Copy link
Member

Salakar commented Apr 16, 2021

Closing in favour of trying the latest messaging version which has since this issue was created been heavily reworked to improve it along with detailed documentation on how to correctly setup your application: https://firebase.flutter.dev/docs/messaging/overview

If you still have a problem please raise a new GitHub issue with up to date information and code snippets if possible. Thanks.

@Salakar Salakar closed this as completed Apr 16, 2021
@firebase firebase locked and limited conversation to collaborators May 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin: messaging Stale Issue with no recent activity type: documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

9 participants