The Problem
We have to try get Mobile network name programatically using Flutter. We attempted to achieve our desired result using the Flutter Dart language but were unsuccessful. Subsequently, we switched to Android Native (Java) and iOS (Swift) languages, where we finally achieved the desired outcome.
We have write a code to Native and we are using concept of platform specified coding that was working while call native code from dart. this is a one of the advandages of flutter and this is part of code optimization.
This function or concept using Business oriented application for Network restriction of users.many more companies have marketing canditates they are work out of the office.The manager of the company could not monitor whether they were working or not so this founction used to where the employees is their and what are they doing of work
Implementing Mobile Network Name Retrieval in Flutter
With a conceptual understanding and knowledge of Flutter's capabilities, let's dive into implementing mobile network name retrieval within a Flutter mobile application.
Setting Up Platform Channels
Begin by setting up platform channels to establish communication between Dart and native code. Define method channels for invoking platform-specific functionalities related to mobile network name retrieval.
Implementing Native Code
On Android, implement the necessary Java/Kotlin code to access the Telephony Manager and retrieve the mobile network name. Similarly, on iOS, implement Objective-C/Swift code to utilize the Core Telephony framework and obtain the carrier name.
Integrating with Flutter
Integrate the platform-specific code within Flutter using method channels. Invoke the native methods to retrieve the mobile network name and handle the data accordingly within the Flutter application .
While the function for obtaining the mobile network name typically yields a 100% result, there are instances where it may take a few seconds to execute. For example, during network transitions, such as switching between networks, the function may experience a delay of one or two seconds before providing the desired information.
It took us three days to develop this function. On the first day, we delved into the concept analysis, exploring methods to obtain the mobile network name and considering various possibilities. The second day involved addressing some encountered issues, but ultimately, we achieved the precise result we desired. Finally, on the last day, we conducted thorough final testing to ensure the function's reliability.
Android
Below Dart file for how get value from Native(kotlin) file or Native Source
//This method return result
Future String?; getMobileNetworkName() async {
try {
;return await platform.invokeMethod('getMobileNetworkName');
;} on PlatformException catch (e) {
;print("Failed to get mobile network name: ${e.message}");
return null;
;}
;}
Create MobileNetworkInfo.kt kotlin file to Android folder
Path:android/app/src/main/kotlin/com/example/appname/MobileNetworkInfo.kt
// MobileNetworkInfo.kt
package com.example.appname // replace your app id
import android.content.Context
import android.telephony.TelephonyManager
class MobileNetworkInfo(private val context: Context) {
fun getMobileNetworkName(): String? {
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
return telephonyManager.networkOperatorName
}
}
And Then Call MobileNetworkInfo file get Mobile network name in MainActivity.kt File ,this file default created project android folder.
Path:android/app/src/main/kotlin/com/example/appname/MainActivity.kt
package com.example.appname // replace your app id
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugin.common.MethodChannel
import android.content.Context
import android.telephony.TelephonyManager
class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val mobileNetworkInfo = MobileNetworkInfo(applicationContext)
val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "your_channel_name")
channel.setMethodCallHandler { call, result ->
if (call.method == "getMobileNetworkName") {
result.success(mobileNetworkInfo.getMobileNetworkName())
} else {
result.notImplemented()
}
}
}
}
IOS
Below Dart file for how get value from Ios Native(Swift) file or Native Source
//This method return result
Future<List<String>> getIosNetworkInfo() async {
const platform = MethodChannel('your_channel_name');
try {
//
final result = await platform.invokeMethod('getSwiftVariable');
return result;
} on PlatformException catch (e) {
print('Failed to get Swift variable: ${e.message}');
return e.message;
}
}
Your AppDelecate.Swift File Like This
Path: Project/ios/Runner/AppDelegate.swift
import UIKit
import Flutter
import CoreTelephony
// import flutter_background_service_ios
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
let channel = FlutterMethodChannel(name: "your_channel_name", binaryMessenger: controller.binaryMessenger)
channel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "getSwiftVariable" {
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
// Get carrier name
let carrierName = carrier?.carrierName
let swiftVariable = carrierName
result(swiftVariable)
} else {
result(FlutterMethodNotImplemented)
}
})
GeneratedPluginRegistrant.register(with: self)
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}