i am using firebase messaging to deliver notifications to my app. However, my app needs some customization to our notification and wants it to support big text style notification.
to make this possible i installed flutter local notification
The problem i'm facing is that flutter local notifications does not show when the app is in the background or terminated.
Here's my code
Future<void> main() async {
// needed if you intend to initialize in the `main` function
WidgetsFlutterBinding.ensureInitialized();
await _configureLocalTimeZone();
// firebase initialize
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
await FirebaseMessaging.instance.subscribeToTopic('debug');
final NotificationAppLaunchDetails? notificationAppLaunchDetails = !kIsWeb &&
Platform.isLinux
? null
: await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
String initialRoute = HomePage.routeName;
if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) {
selectedNotificationPayload =
notificationAppLaunchDetails!.notificationResponse?.payload;
initialRoute = SecondPage.routeName;
}
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('ic_stat_notification');
/// Note: permissions aren't requested here just to demonstrate that can be
/// done later
runApp(
MaterialApp(
initialRoute: initialRoute,
routes: <String, WidgetBuilder>{
HomePage.routeName: (_) => HomePage(notificationAppLaunchDetails),
SecondPage.routeName: (_) => SecondPage(selectedNotificationPayload)
},
),
);
}
and here's my background message handler
// firebase background message handler
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print('A Background message just showed up : ${message.messageId}');
const BigTextStyleInformation bigTextStyleInformation =
BigTextStyleInformation(
'Lorem <i>ipsum dolor sit</i> amet, consectetur <b>adipiscing elit</b>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
htmlFormatBigText: true,
contentTitle: 'News Shot notification',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true,
);
// final String largeIconPath = await downloadAndSaveFile(
// 'https://dummyimage.com/128x128/00FF00/000000', 'largeIcon');
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('big text channel id', 'big text channel name',
// largeIcon: FilePathAndroidBitmap(largeIconPath),
channelDescription: 'big text channel description',
styleInformation: bigTextStyleInformation);
NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
await flutterLocalNotificationsPlugin.show(
id++, 'big text title', 'silent body', notificationDetails);
}
Here's some things i have noticed:
- Messages show when i test them locally when in foreground using icons and sounds.
- The print command does not show in the terminal when the app is in background (maybe the background message is not called).
- fcm is working just fine when tested without flutter_local_notifications.
Have tried all other solution on the internet, none of them are working and handler and main() and handler() functions are same in all of them.