Firebase Cloud Messaging development and release profile

后端 未结 4 951
误落风尘
误落风尘 2020-12-29 11:29

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

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 12:20

    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]
    

提交回复
热议问题