When I am sending a message from the Firebase console, In the foreground state it comes with a popup message. But the problem comes when the app is in a background state. The message comes in the status bar but not popUp on the screen.
Here is my notification service code.
Future<void> initLocalNotification(
BuildContext context, RemoteMessage message) async {
var androidInitialaztionSetting = new AndroidInitializationSettings(
'@mipmap/ic_launcher',
);
var iosInitialaztionSetting = new DarwinInitializationSettings();
var initializationSettings = InitializationSettings(
android: androidInitialaztionSetting,
iOS: iosInitialaztionSetting,
);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification
badge: true,
sound: true,
);
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (payload) {
handleMessage(context, message);
},
);
showNotification(message);
}
Here is another
void showNotification(RemoteMessage message) async {
// AndroidNotificationChannel channel = AndroidNotificationChannel(
// Random.secure().nextInt(100000).toString(), 'No name',
// importance: Importance.max);
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
description:
'This channel is used for important notifications.', // description
playSound: true,
showBadge: true,
enableLights: true,
enableVibration: true,
importance: Importance.max,
);
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
channel.id,
channel.name,
playSound: true,
enableLights: true,
enableVibration: true,
channelDescription: 'your channel description',
importance: Importance.high,
priority: Priority.high,
ticker: 'ticker',
largeIcon: const DrawableResourceAndroidBitmap("ic_launcher"),
styleInformation: const MediaStyleInformation(
htmlFormatContent: true,
htmlFormatTitle: true,
),
);
DarwinNotificationDetails darwinNotificationDetails =
const DarwinNotificationDetails(
presentAlert: true, presentBadge: true, presentSound: true);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: darwinNotificationDetails,
);
await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
Future.delayed(Duration.zero, () {
_flutterLocalNotificationsPlugin.show(
1, // id
message.notification!.title, // title
message.notification!.body, // body
notificationDetails,
);
});
}
handling the background message from top-level function.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// for handling background messages
FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundHandler);
runApp(const MyApp());
}
@pragma('vm:entry-point')
Future<void> _firebaseBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
debugPrint(message.notification!.title);
}