g123k / flutter_app_badger

Support to update the app badge on the launcher (both for Android and iOS)
https://pub.dev/packages/flutter_app_badger
Apache License 2.0
308 stars 161 forks source link

Does not work in background mode android on background message function #23

Closed wfybiz closed 4 years ago

wfybiz commented 4 years ago

I tried it in background message like this but throws errors {Unhandled Exception: MissingPluginException(No implementation found for method updateBadgeCount on channel g123k/flutter_app_badger)}

Future myBackgroundMessageHandler(Map<String, dynamic> message) async { FlutterAppBadger.updateBadgeCount(1); }

But works perfectly in here. Gives problem only in background message function

Widget build(BuildContext context) { _firebaseMessaging.configure( onMessage: (Map<String, dynamic> message) async { FlutterAppBadger.updateBadgeCount(1); },); }

randyhammond commented 4 years ago

Were you able to get this working from a background message? If so, how?

wfybiz commented 4 years ago

ya I fixed it by registering the badge plugin with the FCM plugin. You can see the approach below:

package com.example.io;

import io.flutter.plugin.common.PluginRegistry import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin import fr.g123k.flutterappbadger.FlutterAppBadgerPlugin

object FirebaseCloudMessagingPluginRegistrant { fun registerWith(registry: PluginRegistry?) { if (alreadyRegisteredWith(registry)) { return } FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")); FlutterLocalNotificationsPlugin.registerWith(registry?.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin")); FlutterAppBadgerPlugin.registerWith(registry?.registrarFor("fr.g123k.flutterappbadger.FlutterAppBadgerPlugin")); }

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

}

randyhammond commented 4 years ago

Thanks, I just figured this out and was about to comment the same. I appreciate the response!