Firebase Cloud Messaging development and release profile

12,729

Solution 1

I solved the problem by adding the code below to the project.

FIRInstanceIDAPNSTokenType.Sandbox will be used when you install the app though TestFlight,
and FIRInstanceIDAPNSTokenType.Prod when your app goes live on the App Store.

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
{
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod)
}

Solution 2

I followed the documentation provided and was having the same issue, then I tried the quick start app and it worked. The key seems to be to add the logic to connect to the FCM after obtaining a token, this step is missing in the setup documentation. After I did this it worked on my development device outside of TestFlight without any other special sandbox switches.

https://github.com/firebase/quickstart-ios/blob/master/messaging/FCM/AppDelegate.m#L85

// [START refresh_token]
- (void)tokenRefreshNotification:(NSNotification *)notification {
  // Note that this callback will be fired everytime a new token is generated, including the first
  // time. So if you need to retrieve the token as soon as it is available this is where that
  // should be done.
  NSString *refreshedToken = [[FIRInstanceID instanceID] token];
  NSLog(@"InstanceID token: %@", refreshedToken);

  // Connect to FCM since connection may have failed when attempted before having a token.
  [self connectToFcm];

  // TODO: If necessary send token to appliation server.
}
// [END refresh_token]

// [START connect_to_fcm]
- (void)connectToFcm {
  [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Unable to connect to FCM. %@", error);
    } else {
      NSLog(@"Connected to FCM.");
    }
  }];
}
// [END connect_to_fcm]

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [self connectToFcm];
}

// [START disconnect_from_fcm]
- (void)applicationDidEnterBackground:(UIApplication *)application {
  [[FIRMessaging messaging] disconnect];
  NSLog(@"Disconnected from FCM");
}
// [END disconnect_from_fcm]

Solution 3

Be safe, use below:

    #if DEBUG
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .sandbox)
    #else
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .prod)
    #endif

Do not unnecessarily set sandbox token to prod type and vice versa.

Solution 4

It is about the setAPNSToken() function. You need to set the FIRInstanceIDAPNSTokenType to Prod while adding the device token. I use swift for that, the code I used is this:

func application(application: UIApplication,
                 didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)
{
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Prod)
}

also if you just want to remove the warning, you may use a production provisioning profile.

Share:
12,729
rcpfuchs
Author by

rcpfuchs

Updated on July 18, 2022

Comments

  • rcpfuchs
    rcpfuchs almost 2 years

    I recendly swtiched over from Google Cloud Messaging to Firebase Cloud Messaging.

    With GCM I had to choose the sandbox option. As described here : https://developers.google.com/cloud-messaging/ios/client#obtain_a_registration_token see point 3.

    To receive push notifications in debug mode I had to do something like this

    [[GGLInstanceID sharedInstance] startWithConfig:instanceIDConfig];
    _registrationOptions = @{kGGLInstanceIDRegisterAPNSOption:deviceToken,
                             kGGLInstanceIDAPNSServerTypeSandboxOption:@YES};
    

    To receive push notifications in an App from the AppStore (e.g. TestFlight) I had to say:

    kGGLInstanceIDAPNSServerTypeSandboxOption:@NO};
    

    Now I can not find something like this in Firebase. First I thought great no switching these stupid values anymore. But now I do not receive any push notifications in my TestFlight apps anymore.

    In my debug console when I debug on the device one output is like this:

    <FIRInstanceID/WARNING> APNS Environment in profile: development
    

    Which is good for local debugging, but unwanted in TestFlight. (I do not know if this happens for TestFlight apps, since I do not have a console for them.)

    So my question is: Does anybody know if I can manually change this Sandbox option in Firebase anyhow?

    Thanks,

    Simon