firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.41k stars 1.42k forks source link

Firebase Cloud Messaging not working with visionOS #13173

Closed mark-kinoshita closed 2 days ago

mark-kinoshita commented 4 days ago

Description

I have configured remote notifications for my cross-platform SwiftUI app, everything functions correctly and I'm receiving notifications on iOS devices but receive apns errors when launching on visionOS.

ERROR: 10.28.0 - [FirebaseMessaging][I-FCM023014] No aps-environment set. If testing on a device APNS is not correctly configured. Please recheck your provisioning profiles. If testing on a simulator this is fine since APNS doesn't work on the simulator.

I do successfully received the device token from fcm. These errors do not occur on iOS. I have also tested this on a clean project and had the same results.

Reproducing the issue

Using standing setup for a SwiftUI app will produce the errors when targeting visionOS

`import SwiftUI import FirebaseCore import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    application.registerForRemoteNotifications()
    registerForPushNotifications(application: application)

    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    print("Device Token: \(token)")
    Messaging.messaging().apnsToken = deviceToken
}

func registerForPushNotifications(application: UIApplication) {
    UNUserNotificationCenter.current().delegate = self
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

    UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { granted, error in
        if let error = error {
            print("Failed to request authorization: \(error.localizedDescription)")
            return
        }
        guard granted else { return }
        DispatchQueue.main.async {
            application.registerForRemoteNotifications()
        }
    }
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    completionHandler([.banner, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    completionHandler()
}

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
    print("Token: \(fcmToken)")
}

}

@main struct NotificationsApp: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
    WindowGroup {
        ContentView()
    }

    ImmersiveSpace(id: "ImmersiveSpace") {
        ImmersiveView()
    }
}

}`

Firebase SDK Version

10.28.0

Xcode Version

15.2

Installation Method

Swift Package Manager

Firebase Product(s)

Messaging

Targeted Platforms

iOS, visionOS

Relevant Log Output

10.28.0 - [FirebaseMessaging][I-FCM023014] No aps-environment set. If testing on a device APNS is not correctly configured. Please recheck your provisioning profiles. If testing on a simulator this is fine since APNS doesn't work on the simulator.

<Error>: 10.28.0 - [FirebaseMessaging][I-FCM023014] No aps-environment set. If testing on a device APNS is not correctly configured. Please recheck your provisioning profiles. If testing on a simulator this is fine since APNS doesn't work on the simulator.

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
```json Replace this line with the contents of your Package.resolved. ```

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
```yml Replace this line with the contents of your Podfile.lock! ```
google-oss-bot commented 4 days ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

andrewheard commented 3 days ago

Thank you for the detailed issue report, @mark-kinoshita! Your mention of aps-environment got me looking at https://github.com/firebase/firebase-ios-sdk/blob/04f04917927da92f8f09e51df89328271def076c/FirebaseMessaging/Sources/FIRMessagingUtilities.m#L31-L36 and I noticed TARGET_OS_VISION wasn't listed (TARGET_OS_IOS used to cover visionOS too). I don't have a Vision Pro to test this on myself but I think #13176 will fix the issue. Once this gets merged to main, would you be willing to try it out by temporarily setting your firebase-ios-sdk Swift Package Dependency rule to the main branch?

mark-kinoshita commented 3 days ago

@andrewheard appreciate you looking into this so quickly! I can definitely do that👍🏽

andrewheard commented 3 days ago

Thanks, @mark-kinoshita! This is merged into main now. Please re-open if you're still running into the issue.

mark-kinoshita commented 3 days ago

@andrewheard switched to main, unfortunately I'm still seeing the same 'No aps-environment set.' error

andrewheard commented 3 days ago

Sorry to hear that @mark-kinoshita, any chance you get any useful debugging information with -FIRDebugEnabled passed as a launch argument in Xcode?

mark-kinoshita commented 3 days ago

@andrewheard it looks like I'm getting this error: 10.29.0 - [FirebaseSessions][I-SES000000] Found unknown OSName: "visionos" while converting

Not sure if its related to FCM

andrewheard commented 3 days ago

@andrewheard it looks like I'm getting this error: 10.29.0 - [FirebaseSessions][I-SES000000] Found unknown OSName: "visionos" while converting

Not sure if its related to FCM

@mark-kinoshita Unfortunately I think this is unrelated to FCM (but can be safely ignored).

paulb777 commented 3 days ago

@andrewheard If you haven't already, it looks like a full review of all the #if TARGET_OS_ ... checks are needed in Messaging. For example, handleIncomingLinkIfNeededFromMessage looks suspicious.

andrewheard commented 2 days ago

@mark-kinoshita, just wanted to let you know that I added some possible fixes in #13184 (still just in main) that you might be interested in trying out.

A couple other random things to double check would be that your entitlements are added if you have a separate target for the visionOS version of your app and that the provisioning profile set in Signing & Capabilities is reasonable for visionOS.

mark-kinoshita commented 2 days ago

@andrewheard just switched back to main and its working! I really appreciate your help on this and how quickly this was resolved 👏🏽

andrewheard commented 2 days ago

Thanks for letting me know, @mark-kinoshita! Glad to hear it worked. These fixes will go out in the next Firebase release (10.29 -- around 2 weeks from now).