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

Viewed 66

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.

1 Answers
Related