flutter remote push notification(fcm) not working IOS - ios

android works well
2.from firebase site to iOS real device works well (below this push to ios is ok)
3. remote push from iOS to andriod works well
but remote push message to iOS not working
my Xcode
my info
my 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() //add this before the code below
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}

Actually I had faced this issue , after long research realised that I haven't permit permission for notification, so used permission_handler plugin to ask permission for remote notification .
like this:
Future<Map<Permission, PermissionStatus>> requestPermission() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.notification
].request();
return statuses;
}
Uninstall the application
add above code
Call this function at starting
give permission

Related

Flutter Firebase Messaging (Push Notifications) Not Working for iOS in an Already Existing Application

Firebase messaging works perfectly for Android, but the iOS build cannot receive notifications. I followed this tutorial to add push notifications to the application: https://www.youtube.com/watch?v=u-7ut-phOrA. The only difference is that I already had an App Identifier in the Apple Developer Portal. So instead of creating a new identifier, I enable push notification on the existing identifier.
The permissions are asked in the application as such:
void requestAndRegisterNotification() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
NotificationSettings settings =
await FirebaseMessaging.instance.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print(
'Message title: ${message.notification?.title}, body: ${message.notification?.body}, data: ${message.data}');
});
} else {
print('User declined or has not accepted permission');
}
}
The function was called in the InitState, and I can see the app is requesting permissions when testing.
The certificate in the Developer Portal looks like this: Certificate. The Indentifier looks like this: Identifier. The Key for the push notification looks like this: Key. The Firebase Cloud Messasing APNs looks like this: Firebase.
The AppDelegate file in the Runner has been modified to this:
import UIKit
import Flutter
import Firebase
import FirebaseMessaging
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
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)
}
}
I'm not able to understand where the problem is coming from as there is no clear error message anywhere. The Android build works as expected, but iOS is not even recieving the push notification with a physical device. Even when I try to send the notification to a specific FCM token directly from Firebase.
according to Firebase documentation Enable the Background fetch and the Remote notifications background execution modes

Flutter : How to disable screenshot on iOS devices or just detect the action of screenshot

My problem is that I'am currently stuck and I can't block the screenshots on iOS devices in my flutter app even if I use this solution :
https://pub.dev/packages/screenshot_callback this package is depricated and I can't use it.
The plugin `screenshot_callback` uses a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to see if this plugin supports the Android V2 embedding. Otherwise, consider removing it since a future release of Flutter will remove these deprecated APIs.
If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.
and even adding a swift code doesn't work :
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// <Add>
override func applicationWillResignActive(
_ application: UIApplication
) {
self.window.isHidden = true;
}
override func applicationDidBecomeActive(
_ application: UIApplication
) {
self.window.isHidden = false;
}
}
Please let me know if you have any solutions to fix it :)

How to register plugins using GeneratedPluginRegistrant

How do i register all my plugins when presenting a view controller with implicit engine?
https://flutter.dev/docs/development/add-to-app/ios/add-flutter-screen#alternatively---create-a-flutterviewcontroller-with-an-implicit-flutterengine
When you create new project in Flutter it already ships with code the registers the plugin. Check if you have following;
Android
In your Android project find MainActivity.kt.
Important: Notice that FlutterActivity exist in both io.flutter.embedding.android and io.flutter.app but first one automatically registers all the plugins.
package com.example.myapp
import io.flutter.embedding.android.FlutterActivity
// do not import io.flutter.app.FlutterActivity instead
class MainActivity: FlutterActivity() {
}
iOS
In your iOS project find AppDelegate.swift
import UIKit
import Flutter
import FlutterPluginRegistrant
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self.flutterEngine)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
P.S. Make sure you update the Flutter version to get most of it.
iOS reference

Google Map is not loading in IOS device in Flutter

Google Map is not loading in IOS device in flutter, But it's showing properly in Android device.
I am using package google_maps_flutter 0.5.24+1
My ios/Runner/AppDelegate.swift Code:
import UIKit
import Flutter
import GoogleMaps
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
//(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
}
I have changed Info.plist according to docs:
<key>io.flutter.embedded_views_preview</key>
<true/>
NOTE: I HAVE NO RESTRICTION ENABLED IN API
And Getting no error in console
Please give a solution if anyone faces this issues.
Thanks
It seems like there is some configuration problem from iOS side,
Please double check iOS configuration from package readme.
Please make sure that you also have enabled Maps SDK for iOS in Google Developers Console under Additional APIs section.

How to use Firebase Remote Config in AppDelegate.swift?

Based on advice, I would like to manage API keys using Firebase Remote Config to avoid hard-coding API keys like google_maps_flutter suggests. It has an AppDelegate.swift like:
import UIKit
import Flutter
import GoogleMaps
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("YOUR KEY HERE")
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
How can the above be modified to fetch the API key from Firebase Remote Config and then pass it to GMSServices?
Based on this article, I came up with:
import UIKit
import Firebase
import Flutter
import GoogleMaps
//import os.log
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
RemoteConfig.remoteConfig().fetchAndActivate() { status, error in
let apiKey : String = RemoteConfig.remoteConfig()["Google_Maps_SDK_for_iOS_API_KEY"].stringValue ?? "MISSING";
// os_log("Google_Maps_SDK_for_iOS_API_KEY = '%#'", apiKey)
GMSServices.provideAPIKey(apiKey)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Is there a better way?
I was thinking of making this as a comment, but decided to make it as my answer as it was too long.
I think it's one way of getting keys from remoteConfig, but again failure can happen where fetch config was not retrieved. One way to solve it is by having a force refresh when it fails, but then again you are relying on firebase for those keys and if for some reason (experienced it before firebase was acquired by google) it went down your app then will be unuseable (so as for many apps).
For me, I still put my API keys bundled with the app just to make sure all the important functionality works.
Another option will be having your API Keys bundled and then having a WebService call to check for new keys once the current keys you have has expired/change. That way you have the capability to immediately expire your keys and change it to another one.

Resources