2

I am a facing a strange issue in ios firebase push notification. I am getting a notification on device whenever I run the app from Xcode or even when I create an archive I am successfully getting notifications.

But when I run a release build on device or upload to app store push notifications are not working for me and I am getting the error below

Certificates for both development and production are uploaded and are valid.

{
    "multicast_id": 6319718121948146737,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "InvalidRegistration"
        }
    ]
}

second time when I call API with same token then instead of InvalidRegistration I get NotRegistered error

This is how I am generating fcm token

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
    }

  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        // Pass device token to auth

        #if DEVELOPMENT
        Auth.auth().setAPNSToken(deviceToken, type: .sandbox)
        Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
        #else
        Auth.auth().setAPNSToken(deviceToken, type: .prod)
        Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
        #endif

        Messaging.messaging().apnsToken = deviceToken
        Messaging.messaging().shouldEstablishDirectChannel = true


        //tried this 
        let token = Messaging.messaging().fcmToken
        print("Firebase registration token: \(token)")

        //tried this also both are giving same token but none of them is 
        //working in release build
        let refreshedToken = InstanceID.instanceID().token()
        print("InstanceID token: \(refreshedToken)")


    }
Abhishek
  • 1,654
  • 2
  • 18
  • 31
  • I never used FCM but this seems like a Sandbox/Production issue, let me know if this link helps https://stackoverflow.com/a/54175003/6330448 and you can also refer to this question https://stackoverflow.com/questions/41169460/firebase-push-notifications-not-working-for-production-on-ios – tryKuldeepTanwar Apr 30 '19 at 12:23
  • I have already followed your second link previously but still not helped. I am doing the same way – Abhishek Apr 30 '19 at 12:29
  • try to create production cert without private key, there is nothing you're missing here. – tryKuldeepTanwar Apr 30 '19 at 13:12
  • I tried to send a notification to my APNs device token using APNs certificate but It gave me Invalid device token but in the sandbox environment it is working perfectly fine @dreamBegin – Abhishek May 01 '19 at 10:23
  • Yes, that what i see in most of the question because they were importing there ceritificate with the private key, did you try that excluding your private key? – tryKuldeepTanwar May 01 '19 at 10:29
  • yes i tried that way also same error i am getting @dreamBegin – Abhishek May 01 '19 at 10:31
  • thats the only thing i found here otherwise your code looks fine to me, here he mentioned the same https://stackoverflow.com/a/41696152/6330448 – tryKuldeepTanwar May 01 '19 at 10:33
  • see this issue maybe it'll help you out https://github.com/NativeScript/push-plugin/issues/122 – tryKuldeepTanwar May 01 '19 at 10:39
  • now the thing is I got the notification from APNs as well in production envrmnt but still I am not getting notifications from firebase @dreamBegin – Abhishek May 01 '19 at 10:47
  • froms APNS the private key will not be a issue in production env but in firebase it does creates problem according to the sources here he say legacy build settings solved his issue https://github.com/phonegap/phonegap-plugin-push/issues/2518#issuecomment-426244382 – tryKuldeepTanwar May 01 '19 at 10:54
  • "InvalidRegistration" Clearly specify that you don't have correct FCM token see this here, https://firebase.googleblog.com/2017/01/debugging-firebase-cloud-messaging-on.html there must be a problem with the firebase configuration and your app. – tryKuldeepTanwar May 01 '19 at 11:07
  • but this is the case then it should not work in sandbox mode also but it is working in sandbox mode – Abhishek May 01 '19 at 11:36
  • Configuration for sandbox and production may worry at firebase since i didn't use it i can't tell you more about that. – tryKuldeepTanwar May 01 '19 at 11:39
  • no that same it is not diffrent at firebase end previously with same code notifications were working for me with sandbox and production from firebase suddenly it stopped working – Abhishek May 01 '19 at 12:11
  • that's all i could tell you from here, last suggestion would be to check to repository for the changes when it was working and when it stoped if that remains the same then you know all the problem is from firebase console. – tryKuldeepTanwar May 01 '19 at 12:36

1 Answers1

0

Finally, I solved this issue by removing this part from my code

 #if DEVELOPMENT
 Auth.auth().setAPNSToken(deviceToken, type: .sandbox        
 Messaging.messaging().setAPNSToken(deviceToken, type: .sandbox)
 #else
 Auth.auth().setAPNSToken(deviceToken, type: .prod)
 Messaging.messaging().setAPNSToken(deviceToken, type: .prod)
 #endif
Abhishek
  • 1,654
  • 2
  • 18
  • 31