Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Messaging development and release profile

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

like image 521
rcpfuchs Avatar asked Jun 04 '16 08:06

rcpfuchs


People also ask

What are the two types of messages in Firebase Cloud Messaging?

Using Firebase Cloud Messaging, we can send three types of messages, i.e., Notification Message, Data Message, and the message with both Notification & Data Payload.

Is Firebase FCM free?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.

What is FCM explain in detail?

Firebase Cloud Messaging (FCM), formerly called Google Cloud Messaging (GCM), is a free cloud service from Google that allows app developers to send notifications and messages to users across a variety of platforms, including Android, iOS and web applications.


4 Answers

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)
}
like image 172
Kavin Kumar Arumugam Avatar answered Oct 21 '22 09:10

Kavin Kumar Arumugam


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]
like image 32
Al C Avatar answered Oct 21 '22 08:10

Al C


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.

like image 27
Shobhit C Avatar answered Oct 21 '22 09:10

Shobhit C


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.

like image 27
ulsc Avatar answered Oct 21 '22 09:10

ulsc