I am developing a flutter app with Firebase messaging involved. The app is for both iOS and Android.
In the Android version, I can get data messages pretty well and the Firebase Messaging background listener works well. The background listener get fired every time when a message is sent.
However in iOS the background listener never get fired. But if I send a push notification with notification section included then the default notification is displayed by the SDK, still nothing happens in the background messaging listener.
Reading many questions I figured many has faced this issue. Below is my code.
Main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage? message) {
developer.log("REMOTE MESSAGE");
if (message != null) {
developer.log(message.data.toString());
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
developer.log("REMOTE MESSAGE LISTENER");
if (message.data["data_type"] == "TEXT") {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: UniqueKey().hashCode,
groupKey: message.data["senderUid"],
channelKey: 'basic_channel',
title: message.data["title"],
body: message.data["body"],
summary: message.data["body"], // Anything you want here
notificationLayout: NotificationLayout.Messaging,
displayOnBackground: true,
displayOnForeground: true),
);
} else if (message.data["data_type"] == "IMAGE") {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: UniqueKey().hashCode,
groupKey: message.data["senderUid"],
channelKey: 'basic_channel',
title: message.data["title"],
body: Emojis.art_framed_picture + " " + message.data["body"],
summary: message.data["body"], // Anything you want here
notificationLayout: NotificationLayout.Messaging,
displayOnBackground: true,
displayOnForeground: true),
);
}
});
//Request permission for firebase messaging
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
AwesomeNotifications().initialize('resource://drawable/logo', [
NotificationChannel(
channelGroupKey: 'basic_tests',
channelKey: 'basic_channel',
channelName: 'Basic notifications',
channelDescription: 'Notification channel for basic tests',
defaultColor: Color(0xFF9D50DD),
ledColor: Colors.white,
importance: NotificationImportance.High),
]);
runApp(MultiProvider(
child: MyApp(),
));
}
AppDeligate.swift
import UIKit
import Flutter
import GoogleMaps
import FirebaseMessaging
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("xxxxxxx-xxxx")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}
pubspec.yaml
name: project
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
intl: ^0.17.0
firebase_auth: ^3.1.0
firebase_core: ^1.6.0
firebase_messaging: ^11.4.1
cloud_firestore: ^3.1.9
firebase_storage: ^10.2.8
awesome_notifications: ^0.6.21
dependency_overrides:
firebase_messaging_platform_interface: ^3.5.1
firebase_crashlytics_platform_interface: 3.1.13
cloud_firestore_platform_interface: 5.4.13
firebase_auth_platform_interface: 6.1.11
firebase_storage_platform_interface: 4.0.14
cloud_functions_platform_interface: 5.0.21
firebase_analytics_platform_interface: 3.0.5
firebase_remote_config_platform_interface: 1.0.5
firebase_dynamic_links_platform_interface: 0.2.0+5
firebase_performance_platform_interface: 0.1.0+5
firebase_app_installations_platform_interface: 0.1.0+6
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/graphics/
- assets/icons_chat/
- assets/lottie/
Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>customer</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(FLUTTER_BUILD_NAME)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Use your device location to find nearby sellers</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Use your device location to find nearby sellers</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Use your device location to find nearby sellers</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Take image from your gallery</string>
<key>NSCameraUsageDescription</key>
<string>Take image from your gallery</string>
<key>NSMicrophoneUsageDescription</key>
<string>Take image from your gallery</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
<key>GoogleUtilitiesAppDelegateProxyEnabled</key>
<false/>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
</dict>
</plist>
As you can see here I have enabled Background Fetch and Remote Notifications as well.
When setting up the firebase setup for iOS, I have followed my setups up to here - https://firebase.flutter.dev/docs/messaging/apple-integration/#3-generating-a-provisioning-profile. That means Registering the APN key with Firebase, Registering the App identifier (XCode generated the correct identifier and i am using it), and even generated a Provisioned Profile (However I am using XCode managed provisioned profile). I did not generate any specific certificate anyway, because it is already done by XCode.
Below is my flutter doctor result
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.4 21F79 darwin-arm, locale
en-LK)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 13.4.1)
! CocoaPods 1.10.1 out of date (1.11.0 is recommended).
CocoaPods is used to retrieve the iOS and macOS platform side's plugin
code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To upgrade see
https://guides.cocoapods.org/using/getting-started.html#installation for
instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.67.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
This is my JSON Notification Data
"message": {
"token": recieverFcm,
"data": {
"title": senderName,
"body": message,
"chatRoomId": chatRoomId,
"sender_profile_pic": senderProfilePic,
"senderUid": senderUid,
"data_type": messageType,
"click_action": "OPEN_CHAT_ROOM"
},
"android": {
"priority": "high"
},
"apns": {
"payload": {
"aps": {
"category": "OPEN_CHAT_ROOM",
"sound": "enable",
"content-available": 1,
},
"data": {
"title": senderName,
"body": message,
"chatRoomId": chatRoomId,
"sender_profile_pic": senderProfilePic,
"senderUid": senderUid,
"data_type": messageType,
"click_action": "OPEN_CHAT_ROOM"
},
}
}
}
In above Json I have 2 data sections just for testing. Removing any of them wont make any difference, I have tested.
How can I fix this issue?





