firebase / firebase-ios-sdk

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

Push Notifications Not Working for Catalyst #5048

Closed girishw closed 4 years ago

girishw commented 4 years ago

I am unable to get push notifications working for Catalyst. My current setup works correctly for iOS but shows an authentication error for Catalyst.

Code Excerpts

AppDelegate
      FirebaseApp.configure(options: options)
        Messaging.messaging().delegate = self
        UNUserNotificationCenter.current().delegate = self

      func registerForPushNotifications(completionHandler : @escaping () -> Void) {
        UNUserNotificationCenter.current().getNotificationSettings() { settings in
            guard settings.authorizationStatus == .authorized else { return }

            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
                iOSNotificationService.shared.getPendingNotifications()
                completionHandler()
            }
        }
    }

func requestPushNotificationAuthorization(completionHandler: @escaping ()-> Void ) {
        let authOptions : UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { (granted, error) in
            guard granted else {
                if let errorString = error?.localizedDescription {
                    os_log("Push notification authorization denied : %{public}s", errorString )
                }

                return
            }
            self.registerForPushNotifications() {
               // 
               //
            }

            completionHandler()
        }
    }

Error { Error: Auth error from APNS or Web Push Service Raw server response: "{"error":{"code":401,"message":"Auth error from APNS or Web Push Service","status":"UNAUTHENTICATED","details":[{"@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode":"THIRD_PARTY_AUTH_ERROR"}]}}" at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:253:16) at Function.FirebaseMessagingError.fromServerError (/srv/node_modules/firebase-admin/lib/utils/error.js:283:16) at Object.createFirebaseError (/srv/node_modules/firebase-admin/lib/messaging/messaging-errors.js:34:47) at /srv/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:76:42 at at process._tickDomainCallback (internal/process/next_tick.js:229:7) errorInfo: { code: 'messaging/unknown-error', message: 'Auth error from APNS or Web Push Service Raw server response: "{"error":{"code":401,"message":"Auth error from APNS or Web Push Service","status":"UNAUTHENTICATED","details":[{"@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode":"THIRD_PARTY_AUTH_ERROR"}]}}"' }, codePrefix: 'messaging' }

Configuration

google-oss-bot commented 4 years ago

I found a few problems with this issue:

morganchen12 commented 4 years ago

The error you're getting is likely to cause push notifications to fail on both iOS and macOS Catalyst. Can you try generating a new auth key and uploading it to Firebase console?

girishw commented 4 years ago

I tried with a new APNS key and am seeing the same result. A few more data points:

girishw commented 4 years ago

I also see the following warning : 6.18.0 - [Firebase/InstanceID][I-IID003014] Running InstanceID on a simulator doesn't have APNS. Use prod profile by default.

morganchen12 commented 4 years ago

@chliangGoogle could InstanceID be mistaking the Catalyst runtime for a simulator environment?

ryanwilson commented 4 years ago

Looks like that's coming right from GoogleUtilities itself - https://github.com/firebase/firebase-ios-sdk/blob/0dd8dda16557cc56fb93e29fac966e889c3d8347/GoogleUtilities/Environment/third_party/GULAppEnvironmentUtil.m#L203-L211

We should be checking TARGET_OS_MACCATALYST here.

ryanwilson commented 4 years ago

Also it looks like the example needs to change, too: https://github.com/firebase/firebase-ios-sdk/blob/e028c72162a3df49ce3147aa2fc2fca4841353da/Example/Messaging/Sample/iOS/Environment.swift#L20-L27

morganchen12 commented 4 years ago

The Swift sample should use #if targetEnvironment(simulator) instead of checking architectures.

morganchen12 commented 4 years ago

This fix will be in the next release, but you can test it out now by installing the pod directly from GitHub.

girishw commented 4 years ago

@morganchen12 This fixed the InstanceID/APNS warning but still hasn't fixed the APNS authentication error.

charlotteliang commented 4 years ago

I can help take a look at. Are you able to reproduce with catalyst?

girishw commented 4 years ago

Yes, I am seeing when I build my app for Catalyst. The same code + configuration built for iOS does not have the problem. Do I need to make any changes to the bundle ID?

charlotteliang commented 4 years ago

Can you share with us the APNS authentication error?

girishw commented 4 years ago

@chliangGoogle

Error: Auth error from APNS or Web Push Service Raw server response: "{"error":{"code":401,"message":"Auth error from APNS or Web Push Service","status":"UNAUTHENTICATED","details":[{"@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode":"THIRD_PARTY_AUTH_ERROR"}]}}" at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:253:16) at Function.FirebaseMessagingError.fromServerError (/srv/node_modules/firebase-admin/lib/utils/error.js:283:16) at Object.createFirebaseError (/srv/node_modules/firebase-admin/lib/messaging/messaging-errors.js:34:47) at /srv/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:76:42 at at process._tickDomainCallback (internal/process/next_tick.js:229:7) errorInfo: { code: 'messaging/unknown-error', message: 'Auth error from APNS or Web Push Service Raw server response: "{"error":{"code":401,"message":"Auth error from APNS or Web Push Service","status":"UNAUTHENTICATED","details":[{"@type":"type.googleapis.com/google.firebase.fcm.v1.FcmError","errorCode":"THIRD_PARTY_AUTH_ERROR"}]}}"' }, codePrefix: 'messaging'

charlotteliang commented 4 years ago

So the error is from server side, meaning your client apns token was not setup correctly. You mentioned that "UNUserNotificationCenter.current().requestAuthorization(options:) does not show a dialog to the user." This seems like your push notification setting in xcode wan't setup correctly. Can you double check that to make sure the dialog got pop out?

From the api, make sure your have Mac Catalyst 13.0+. https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649527-requestauthorization And make sure you enable push notification in capabilities in xcode and setup the signing and provisioning correctly.

charlotteliang commented 4 years ago

can you also check if you are getting apns token right now?

girishw commented 4 years ago

@chliangGoogle

charlotteliang commented 4 years ago

"However, when I build and run for Catalyst, I am seeing the error. " Do you see any error in xcode when you switch? specifically signing errors with your existing push notification provisioning profile?

girishw commented 4 years ago

I see the following errors, not sure if they are related:

[User Defaults] Couldn't write values for keys ( ApplicationAccessibilityEnabled ) in CFPrefsPlistSource<0x600002c21780> (Domain: com.apple.Accessibility, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access

[User Defaults] Couldn't write values for keys ( FullKeyboardAccessFocusRingEnabled ) in CFPrefsPlistSource<0x600002c21780> (Domain: com.apple.Accessibility, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access

[User Defaults] Couldn't write values for keys ( ApplicationAccessibilityEnabled ) in CFPrefsPlistSource<0x600002c21780> (Domain: com.apple.Accessibility, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access

[Firebase/Core][I-COR000008] The project's Bundle ID is inconsistent with either the Bundle ID in 'GoogleService-Info.plist', or the Bundle ID in the options if you are using a customized options. To ensure that everything can be configured correctly, you may need to make the Bundle IDs consistent. To continue with this plist file, you may change your app's bundle identifier to 'com.myApp.bundleIF'. Or you can download a new configuration file that matches your bundle identifier from https://console.firebase.google.com/ and replace the current one.

Couldn't read values in CFPrefsPlistSource<0x600002c51780> (Domain: group.myApp.bundleID, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd

[Firebase/InstanceID][I-IID003014] Error while reading embedded mobileprovision Error Domain=NSCocoaErrorDomain Code=260 "The file “embedded.mobileprovision” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/.../Developer/Xcode/DerivedData/MyApp-bfnufiqtzeksrgetsiycpfkfzngu/Build/Products/Beta-maccatalyst/MyApp.app/embedded.mobileprovision, NSUnderlyingError=0x600000db5fb0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

charlotteliang commented 4 years ago

I see couple of issues here:

  1. Core refuses the bundle ID as it's not in plist @ryanwilson
  2. Messaging can't detect the right mobileprovision file (assigned this issue to myself)
  3. Regardless Firebase, if the dialog is not popping up, it could mean APNS is still not setup correctly on Apple side. Can you try to send a notification using the APNS token to see if it delivers (this will also rule out Apple side of issues)
girishw commented 4 years ago

@chliangGoogle Regarding 3: I am currently using the admin send function as described here: https://firebase.google.com/docs/cloud-messaging/send-message

Do I just substitute the APNS token for the FCM registration token to test 3?

charlotteliang commented 4 years ago

no, you will use Apple's sender API: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns/

charlotteliang commented 4 years ago

You can try something like this:https://github.com/firebase/firebase-ios-sdk/issues/4042#issuecomment-542359566

girishw commented 4 years ago

I used the following to test: https://github.com/onmyway133/PushNotifications

While I don't see the notification delivered on the device, I don't see an error message either. The PushNotifications app shows a success message.

Edit: Correction. The notifications are being delivered. The first notifications were delayed for some reason.

charlotteliang commented 4 years ago

I see so number 3 is not an issue, we are working on issues 1 and 2.

ryanwilson commented 4 years ago

Looking at the bundleID issue now, also related to #5126

charlotteliang commented 4 years ago

@girishw Please try again with xcode 11.4 which allows to use the same bundle ID as your iOS one.

Screen Shot 2020-03-25 at 5 07 26 PM

I managed to get it working after #5165. So if you can enable it without using the bundle ID prefix with maccatalyst and wait for the next release with #5165 should resolve the issue.

girishw commented 4 years ago

@chliangGoogle I rebuilt my app with Xcode 11.4, Use iOS Bundle Identifier checked. I built with the github sources. I now see the following error message:

{ Error: Requested entity was not found. at FirebaseMessagingError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:42:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:88:28) at new FirebaseMessagingError (/srv/node_modules/firebase-admin/lib/utils/error.js:253:16) at Function.FirebaseMessagingError.fromServerError (/srv/node_modules/firebase-admin/lib/utils/error.js:283:16) at Object.createFirebaseError (/srv/node_modules/firebase-admin/lib/messaging/messaging-errors.js:34:47) at /srv/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:76:42 at at process._tickDomainCallback (internal/process/next_tick.js:229:7) errorInfo: { code: 'messaging/registration-token-not-registered', message: 'Requested entity was not found.' }, codePrefix: 'messaging' }

I verified that messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) is being called and I am using the new FCM token.

Do I need to do anything else to reset my app for the new configuration?

charlotteliang commented 4 years ago

Can you reset your app uninstall it and reinstall again?

girishw commented 4 years ago

@chliangGoogle I am running builds from Xcode to test. I tried deleting the user's account and running a first launch process with authorization of notifications, etc. Still the same result :-(

charlotteliang commented 4 years ago

Are you running on mac or ipad? Is it possible to share your debugLog with me? You can send directly to me.

charlotteliang commented 4 years ago

Sync offline and customer didn't get the latest version of code. @girishw please try again with the patch changes of #5165 and reopen the issue if it still doesn't work for you.

boblepepeur commented 4 years ago

same problem for me, i have pod version 6.21 i accept permission to allow push on my catalyst app i dont receive push on IOS it works like a charm

any help ? its something special to do with catalyst ? i add keychain sharing capability as i read it in 6.21 readme, but its change nothing.

charlotteliang commented 4 years ago

The fix is in the next release. Please wait for 6.22 and see if that resolves the issue.

boblepepeur commented 4 years ago

hey hey, i get the 6.22 and test push with cloud messaging test in firebase console, its not working, i don't get notification on my mac with my catalyst app.