Flutterfire: [firebase_messaging] Feature Request: Make firebase_messaging support creating channels from Dart

Created on 13 Oct 2019  ·  20Comments  ·  Source: FirebaseExtended/flutterfire

In Android Oreo, notifications are encouraged to have "channels" so users can control types of notifications more easily. Can this feature be natively supported in the firebase_messaging plugin?

For now, I've managed to solve this problem with a semi-hacky platform channel, as below:

    new MethodChannel(getFlutterView(), "notifications").setMethodCallHandler(
        new MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall call, Result result) {
                if(call.method.equals("registerChannel")) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        String id = call.argument("id");
                        String name = call.argument("name");
                        String description = call.argument("description");

                        NotificationChannel mChannel =
                            new NotificationChannel(id, name,
                                                    NotificationManager.IMPORTANCE_DEFAULT);
                        mChannel.setDescription(description);

                        NotificationManager notificationManager =
                            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                        notificationManager.createNotificationChannel(mChannel);

                        result.success("Notification channel " + name + " created");
                    } else {
                        // No-op
                        result.success("Android version is less than Oreo");
                    }
                }
            }
        });
  }

This works, but doesn't allow us to set notification priorities without significant modifications, and would be better as part of the plugin anyway.

NB: Am willing to make a PR for this later, with the priority functionality as soon as I figure out how to do it (and if others say this is a good idea).

crowd enhancement

Most helpful comment

Flutter Team, please fix this issue ASAP

All 20 comments

@ghost

The issue at https://github.com/flutter/flutter/issues/16022 has been closed and moved here. Future collaboration on this issue will be done here.

still pending???

Any news, cause I want clients to select their own sound from a list of sounds when they send a notification, but then I need to be able to create a channel first on Android. I come from appcelerator and there you can create one or more channels on configuration of the notification listener. It would be nice if we can have something here as well from within Dart in order to create the channels on demand.

@behlsoft but that's only from the kotlin file right? Not from within Dart itself

  1. Integrating Firebase Cloud Messaging in Flutter. (firebase_messaging 5.1.4 : https://pub.dev/packages/firebase_messaging)
  2. Notification Channels are introduced in Android 8.0 (Android Oreo & above), Refer : https://developer.android.com/training/notify-user/channels
  3. But these Notification Channels are not integrated in flutter, which is creating problem in the devices running a Flutter app in device Android 8.0 and above
    What is the problem: Flutter app cannot play custom sound, when app is in Background.
    We can see the “Channel ID” exception in the logs, but the control is with Flutter SDK, so we cannot help it.
  4. This is a serious problem for our business, as we are incurring losses & clients are constantly asking for this feature.

Tried a Native approach:

  1. We tried to start a Service (like in Android Native), but alas not successful.

Thus, requesting to please give some idea

Flutter Team, please fix this issue ASAP

Is anybody from the Flutter team looking into this or is it just that custom notifications isn't the important? Cause for our clients it really is and would have to deviate to somewhere else. We've got customers choosing their own sound, so we need to be able to create a channel if not existing and send a custom sound along

@kroikie could you elaborate or make a gist on how you implemented this? I will try to implement this like you did, but if you would have a gist then it would be much easier.
I will extend it then for my purpose as I want to create channels with sound settings on demand and if sounds needs to be changed (from the backend), the channel will be deleted (so I've got to create another method call to delete the channel, but I will figure that out) and then recreate the channel with the new sound settings.

So it would really help me if you could explain a little bit more how you implemented the code from within flutter. Thanks in advance

Based on the suggestions of @kroikie I've created some scripting to dynamically create the channel with custom sound as well as channelgroups and the option to remove them. What I will do is create a gist and put the link towards it here for future references

Is it still not possible to play custom sound via firebase cloud messasing?

No you can't but what you could do is create a notifcationchannel and if wanted a notificationchannelgroup dynamically as described in my gist:
https://gist.github.com/ErikDohmen/983adc0ad98cd62fbf61c7bf7a476078

No you can't but what you could do is create a notifcationchannel and if wanted a notificationchannelgroup dynamically as described in my gist:
https://gist.github.com/ErikDohmen/983adc0ad98cd62fbf61c7bf7a476078

Thanks for this. What do I do with the default code in MainActivity.kt?

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

@aldwnesx I've updated the gist with an extra setup. You've got a new template for the MainActivity in flutter without the onCreate method. Therefore I've added an extra setup:
https://gist.github.com/ErikDohmen/983adc0ad98cd62fbf61c7bf7a476078#file-mainactivit-kt-new-template-without-oncreate

@aldwnesx @ErikDohmen In flutter, almost everything is a Widget Workaround. 😕

using flutter_local_notification package i was able to create notification channels with custom sounds and send notifications from firebase triggering channel id.

using flutter_local_notification package i was able to create notification channels with custom sounds and send notifications from firebase triggering channel id.

Could you share this? Im searching on how to fix this.

Hi all - it's unlikey this functionality will exist within FlutterFire. The messaging plugin is responsible for handling remote message payloads rather that handling app notification functionality.

That being said, https://pub.dev/packages/flutter_local_notifications exists specifically for these sort of things.

You can see how this is done on the example: https://pub.dev/packages/flutter_local_notifications#-example-tab-

Search for the _createNotificationChannel method.

using flutter_local_notification package i was able to create notification channels with custom sounds and send notifications from firebase triggering channel id.

Could you share this? Im searching on how to fix this.

Follow the example.

Archive.zip

As mentioned above we recommend using a local notifications package to manage channels. Channels management isn't a Firebase API and therefore not something we will support inside FlutterFire.

Was this page helpful?
0 / 5 - 0 ratings