I need to create a notification channel in app side. this notification channel should be priority default when app is active(no banners needed). when the app is in background priority should be high(need to show banner notifications).
I have created a notification channel using flutterLocalNotificationsPlugin
code used create notification channel
Future<void> createNotificationChannel({bool priority = false}) async {
Importance importance = Importance.max;
if (priority) {
importance = Importance.max;
} else {
importance = Importance.defaultImportance;
}
log({"isResumed": priority, "importance": importance.name}.toString());
AndroidNotificationChannel channel = AndroidNotificationChannel(
'sound', // Replace with your channel ID
'sound',
importance: importance,
playSound: true,
);
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}
Code to delete notification channel
Future<void> deleteNotificationChannel({bool isResumed = false}) async {
log("delete channel");
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.deleteNotificationChannel("sound");
await createNotificationChannel();
}
Then i use didChangeAppLifecycleState to detect the application state and update the channel
void didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await deleteNotificationChannel(); // Adjust channel importance to low
} else {
await createNotificationChannel(priority: true); // Use default importance
}
}
However when I send a notification from onesignal dashboard with category "Created in App" and Existing Channel "sound" notification banner will not come just notification in the background.
But if I send a notification with high priority from onesignal dashboard it comes as a banner.
what could be the problem here and how can I fix this.
I used OneSignal for one of my projects and I am pretty sure that you do not need flutterLocalNotificationsPlugin at all, in order to implement that behavior.
Check this example
Also this notifications handlers can be handy. Also check for the code example how to handle push notification when your app is closed.