cordova-plugin-firebase icon indicating copy to clipboard operation
cordova-plugin-firebase copied to clipboard

FirebasePluginMessageReceiverManager integration

Open abarisain opened this issue 6 years ago • 3 comments

Hi,

I'm one of the maintainers of the cordova version of our company's SDK. Part of what we do is push notifications. https://github.com/BatchLabs/cordova-plugin

A while ago, we added explicit compatibility with your plugin, disabling our own implementation of Google's play services plugin, like you had before dropping it. That part is fine.

But now, we noticed that if we send notifications that include a title in the payload, your plugin's receiver shows it with no obvious way to disable it in the documentation. Users end up seeing two notifications: one by our native receiver, the other by yours (with only the title set, no body, which looks awkward). For information, we use "msg" for the message body (which does not trigger your plugin), and "title" for the title.

I noticed that you provide a way to consume the messages before your plugin does, using https://github.com/arnesson/cordova-plugin-firebase/blob/master/src/android/FirebasePluginMessageReceiverManager.java and an implementation of FirebasePluginMessageReceiver.

But I see no documentation about how I'm supposed to implement this. I got from the code that I need to subclass FirebasePluginMessageReceiver, and that simply instanciating it registers it to the manager.

My question is: how do I instanciate it realibely and before your service does its job? As far as I know, Cordova doesn't let you run any native code easily in the Application's onCreate, which would be the perfect place to instanciate my subclass. Can't use a "static" block in the class, because the class loader will never have a reason to load my class.

I tried toying around with a BroadcastReceiver that starts up before your service, but this is very brittle and I don't want this to break because of a priority change, or after an update of this plugin.

Maybe the person who implemented this has a way to use it in mind? Or maybe we could find a way to declare plugins that should be registered in the FirebasePluginMessageReceiverManager declaratively (manifest metadata?)

Note that the need to subclass a Java class adds another hurdle to the integration: people who have our plugin but not yours. We can work this arround by attempting to instanciate our implementation using Class.forName(), but that's something else than plugin discovery on your side could save us from doing, as it spams logs.

abarisain avatar Dec 14 '18 16:12 abarisain

Hi @abarisain , is there any way to extend FirebasePluginMessageReceiver instead of BroadcastReceiver in your project's dist/src/android/BatchCordovaPushReceiver.java ?

Of course you will have to implement the function onMessageReceived that shall return true if the remote message is processed by your reciever.

Check: https://github.com/arnesson/cordova-plugin-firebase/blob/master/src/android/FirebasePluginMessageReceiverManager.java

and

https://github.com/arnesson/cordova-plugin-firebase/blob/master/src/android/FirebasePluginMessageReceiver.java

amirhmoradi avatar Jan 07 '19 19:01 amirhmoradi

Hello,

Sure. I can do that, and I'm willing to.

But once I do that, how can I register my receiver to FirebasePluginMessageReceiverManager? FirebasePluginMessageReceiver does that in its constructor, but that constructor has to be called by something.

This is what my issue is about: How can I integrate with your system after implementing FirebasePluginMessageReceiver, since Cordova does not allow me to hook the Application's onCreate? In a native application, I'd put the code there and call it a day. Another way would be for your plugin to parse the manifest, and automatically register the receivers (or call their constructor) based on specific entries. Then, out plugin would just add a manifest entry (meta-data?) describing the FirebasePluginMessageReceiver implementation, and your plugin will pick it up. I'm also willing to contribute this code if we agree on a solution.

I need something to call this constructor, or at least register my code with the Manager. I believe there is a missing piece in your plugin, or something I'm not quite getting. Is there another project that already used this feature?

I guess I could use a content provider to hack around this, but it really feels dirty to use this trick, and will waste a lot of CPU on nothing.

abarisain avatar Jan 08 '19 08:01 abarisain

Old Topic but needed to do this myself. I added an Application class and defined a FirebasePluginMessageReceiver there. Instantiated in OnCreate

@Override public void onCreate() { super.onCreate(); new Appear2meeMessageReceiver(); // loads into firebase receivers }

I would have liked a similar method to grab the deviceToken.

icyield avatar Feb 05 '20 12:02 icyield