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).
@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
Tried a Native approach:
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.
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.
Most helpful comment
Flutter Team, please fix this issue ASAP