'Notification not working in IOS and no sound When notification is received but vibration works
I have created Flutter application using firebase for notification after integration of firebase for both IOS and android.
Notification only works in android but with no Sound
when notification is send from firebase console to android it works But not for IOS there is no notification in iOS physical device
The issue is with iOS device i am not able to figure out how to solve this issue
Please guide me what to do if i have share any file do let me know which file i have to share
[✓] Flutter (Channel stable, 2.5.3, on macOS 12.1 21C52 darwin-arm, locale en-IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] Connected device (3 available)
• No issues found!
main.dart
import....
const iOSLocalizedLabels = false;
/// Create a AndroidNotificationChannel for heads up notifications
AndroidNotificationChannel channel;
/// Initialize the FlutterLocalNotificationsPlugin package.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
//Device Information Plugin
DeviceInfoPlugin deviceInfo=DeviceInfoPlugin();
// Global Key for Navigating during Notifications
GlobalKey<NavigatorState> navigatorKey=GlobalKey<NavigatorState>();
Map phoneInformation={};
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async
{
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
///For supporting iOS 14+ phone with new updates
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
Future<void> main() async
{
WidgetsFlutterBinding.ensureInitialized();
FlutterAppBadger.removeBadge();
await Firebase.initializeApp();
// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb)
{
channel = const AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
/// Create an Android Notification Channel.
/// We use this channel in the `AndroidManifest.xml` file to override the
/// default FCM channel to enable heads up notifications.
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
///Added new
await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
/// to check the authorization of the application
print('User granted permission');
}
if(Platform.isIOS)
{
IosDeviceInfo iosDeviceInfo=await deviceInfo.iosInfo;
Map<String,String> data=
{
"name":iosDeviceInfo.name,
"isPhysicalDevice":iosDeviceInfo.isPhysicalDevice.toString(),
"uniqueId":iosDeviceInfo.identifierForVendor
};
phoneInformation=data;
print("----------- $data");
}
else
{
AndroidDeviceInfo androidDeviceInfo=await deviceInfo.androidInfo;
Map<String,String> data=
{
"name":androidDeviceInfo.device,
"isPhysicalDevice":androidDeviceInfo.isPhysicalDevice.toString(),
"uniqueId":androidDeviceInfo.androidId,
};
phoneInformation=data;
print("-----------$data");
}
runApp(MyApp());
}
class MyApp extends StatelessWidget{}
AppDelegate.swift
// import UIKit
// import Flutter
// import Firebase
// @UIApplicationMain
// @objc class AppDelegate: FlutterAppDelegate {
// override func application(
// _ application: UIApplication,
// didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
// ) -> Bool {
// FirebaseApp.configure()
// GeneratedPluginRegistrant.register(with: self)
// //GeneratedPluginRegistrant.register(with: self)
// if #available(iOS 10.0, *) {
// UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
// }
// return super.application(application, didFinishLaunchingWithOptions: launchOptions)
// }
// }
import UIKit
import Flutter
import FirebaseMessaging
import Firebase // Add this import
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, MessagingDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//added new comment
override func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
super.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}
}
info.plist
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
<key>FirebaseScreenReportingEnabled</key>
<false/>
At Splash Screen
Future<void> setToken(String token) async
{
print('FCM Token:--- $token');
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString("FCM_token", token);
}
askPermission() async
{
await FirebaseMessaging.instance.requestPermission(
announcement: true,
carPlay: true,
criticalAlert: true,
badge: true,
sound: true,
);
@override
void initState()
{
// TODO: implement initState
super.initState();
FlutterAppBadger.removeBadge();
askPermission();
Timer(Duration(microseconds: 1000), () {setState(()=>_Bigger = true);});
startTime();
FirebaseMessaging.onMessage.listen((RemoteMessage message)
{
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
if (notification != null && android != null && !kIsWeb)
{
flutterLocalNotificationsPlugin.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
// TODO add a proper drawable resource to android, for now using
// one that already exists in example app.
icon: '@mipmap/ic_launcher',
),
));
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
{
print('A new onMessageOpenedApp event was published!');
});
}
});
}
Home.dart
@override
void initState()
{
// TODO: implement initState
super.initState();
FirebaseMessaging.onMessage.listen((RemoteMessage message)
{
if(message.notification.body.contains("requested")==true && widget!=AppNotificationVC())
Navigator.pushNamed(navigatorKey.currentState.context,'/AppNotificationVC');
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message)
{
print('Notification /Message open when click on it');
if(message.notification.body.contains("requested")==true && widget!=AppNotificationVC())
Navigator.pushNamed(navigatorKey.currentState.context,'/AppNotificationVC');
});
Future.delayed(Duration.zero, () async
{
....
});
}
openNotificationCashpotAPI(String cashpot_id) async {...}
notificationBadgeAPI() async{...}
openUsernotificationAPI() async{...}
Solution 1:[1]
SwiftUI's EnvironmentObjects and EnvironmentValues should propagate ONLY to SwiftUI children views. For other (trans-framework/API) cases we have to do this manually, like
struct TabBarController: UIViewControllerRepresentable {
@Environment(\.lineLimit) var lineLimit // << here !!
typealias UIViewControllerType = UITabBarController
func makeUIViewController(context: Context) -> UIViewControllerType {
let uiViewController = UITabBarController()
uiViewController.setViewControllers([
UIHostingController(rootView:
FooBar().environment(\.lineLimit, lineLimit)), // << here !!
UIHostingController(rootView: FooBar()),
UIHostingController(rootView: FooBar())
], animated: false)
return uiViewController
}
// ... other code
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Asperi |
