Mariam Younes
Mariam Younes

Reputation: 389

Why the FCM not working on ios in flutter

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

Upvotes: 1

Views: 1637

Answers (1)

Gbenga B Ayannuga
Gbenga B Ayannuga

Reputation: 2792

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
        }
    
    }
   
  
 
}

Upvotes: 1

Related Questions