In my flutter project, I am getting push notification from firebase. In android device, get notification banner and notification badge on app icon perfectly while app is in background and foreground states. But in iOS device, notification banner and notification badge is appearing while app is in foreground state. For background state, the notification banner is not appearing and the notification badge is not showing.
I have used flutter_local_notification for get and configure push notification and used flutter_app_badger for showing notification count in notification badge on app icon of device home page.
in my main.dart file, I have added a method named _firebaseBackgroundMessagingHandler for handling firebase background message and called that in main() method like this --> FirebaseMessaging.onBackgroundMessage(_firebaseBackgroundMessagingHandler);
@pragma('vm:entry-point')
Future<void> _firebaseBackgroundMessagingHandler(RemoteMessage message) async {
await Firebase.initializeApp();
NotificationServices().showNotifications(message);
}
In notification_service.dart file -->
void firebaseInit(BuildContext context) {
FlutterAppBadger.isAppBadgeSupported();
FirebaseMessaging.onMessage.listen((message) {
print("Notification Title: ${message.notification!.title.toString()}");
print("Notification Body: ${message.notification!.body.toString()}");
print('Custom Data: ${message.data.toString()}');
if (Platform.isAndroid) initLocalNotifications(context, message);
if (Platform.isIOS) foregroundMessage();
showNotifications(message);
});
}
Future<void> showNotifications(RemoteMessage message) async {
//TODO: need to update channel for separate type of notifications
AndroidNotificationChannel channel = AndroidNotificationChannel(
Random.secure().nextInt(1000).toString(),
'High Important Notifications',
importance: Importance.max);
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(channel.id, channel.name,
channelDescription: 'Your channel description',
importance: Importance.max,
priority: Priority.high,
enableVibration: true,
enableLights: true,
playSound: true,
ticker: 'ticker');
DarwinNotificationDetails darwinNotificationDetails =
const DarwinNotificationDetails(
presentAlert: true, presentBadge: true, presentSound: true, presentBanner: true);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails, iOS: darwinNotificationDetails);
String payload = message.data['payload'];
Map<String, dynamic> decodedPayload = jsonDecode(payload);
var messageFrom = decodedPayload['message_from'];
var senderContactName = decodedPayload['sender_contact_name'];
var senderMessage = decodedPayload['message'];
var statusOfFriend = decodedPayload['status'];
FlutterAppBadger.updateBadgeCount(1);
Future.delayed(Duration.zero, () {
_flutterLocalNotificationsPlugin.show(
0,
(statusOfFriend == 1) ? senderContactName : messageFrom,
smallSentence(senderMessage),
notificationDetails);
});
}
Future foregroundMessage() async {
FlutterAppBadger.updateBadgeCount(1);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
I also have added those in info.plist file -->
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
Am I missing something for background processing and banner showing of iOS part?