Why the FCM not working on ios in flutter

1,285
override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let firebaseAuth = Auth.auth()
        firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)

    }

add this to your appDelegate.... here is example of appDelegate

import UIKit
import Flutter

import Firebase
import FirebaseAuth
import UserNotifications
import FirebaseMessaging


@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    

    application.registerForRemoteNotifications()
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
  
   override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let firebaseAuth = Auth.auth()
        firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.unknown)

    }
    override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let firebaseAuth = Auth.auth()
        if (firebaseAuth.canHandleNotification(userInfo)){
            print(userInfo)
            return
        }
    
    }
   
  
 
}
Share:
1,285
Mariam Younes
Author by

Mariam Younes

Updated on December 31, 2022

Comments

  • Mariam Younes
    Mariam Younes over 1 year

    I'm Following this steps to add push notification to my app using firebase messaging but after all of this steps it doesn't work

    ios_client

    APNs

    <key>FirebaseAppDelegateProxyEnabled</key>
    <false/>
    
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
      void firebaseTrigger(BuildContext ctx) async {
        _firebaseMessaging.configure(
          onMessage: (Map<String, dynamic> message) async {
            print("onLaunch: $message");
            Fluttertoast.showToast(
              msg: message['notification']['title'],
              toastLength: Toast.LENGTH_LONG,
              gravity: ToastGravity.TOP,
              timeInSecForIosWeb: 6,
            );
          },
          onLaunch: (Map<String, dynamic> message) async {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => NotificationScreen(),
              ),
            );
          },
          onResume: (Map<String, dynamic> message) async {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => NotificationScreen(),
              ),
            );
          },
        );
      }
    
      @override
      void initState() {
        super.initState();
        firebaseTrigger(context);
        _firebaseMessaging.requestNotificationPermissions(
            const IosNotificationSettings(
                sound: true, badge: true, alert: true, provisional: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });
      }
    

    But the push notification doesn't work for ios only but it works on android so kindlly can anyone help me in my problem

    Edit: my appDelegate

    enter image description here

    • Gbenga B Ayannuga
      Gbenga B Ayannuga over 2 years
      are u runing on simulator?
    • Abhijith
      Abhijith over 2 years
      check the documentation i am not familiar with ios, but go through this also firebase.flutter.dev/docs/messaging/apple-integration
    • Mariam Younes
      Mariam Younes over 2 years
      @GbengaBAyannuga no in real device
    • Mariam Younes
      Mariam Younes over 2 years
      @Assassin I'm following this document but same problem
    • Gbenga B Ayannuga
      Gbenga B Ayannuga over 2 years
      let me see your payload
    • Mariam Younes
      Mariam Younes over 2 years
      @GbengaBAyannuga I'm send the notification from firebase console
  • Mariam Younes
    Mariam Younes over 2 years
    should i remove this if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate } from my appDelegate and put yours ?
  • Gbenga B Ayannuga
    Gbenga B Ayannuga over 2 years
    remove it... after adding it.... update let me see where u paste it
  • Mariam Younes
    Mariam Younes over 2 years
    is this answer will make the ios notification permission appear ??