【问题标题】:Firebase Cloud Messaging development and release profileFirebase 云消息传递开发和发布配置文件
【发布时间】:2016-10-04 08:00:14
【问题描述】:

我最近从 Google Cloud Messaging 切换到 Firebase Cloud Messaging。

使用 GCM,我必须选择沙盒选项。如此处所述:https://developers.google.com/cloud-messaging/ios/client#obtain_a_registration_token 见第 3 点。

要在调试模式下接收推送通知,我必须这样做

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

要在应用程序中从 AppStore(例如 TestFlight)接收推送通知,我不得不说:

kGGLInstanceIDAPNSServerTypeSandboxOption:@NO};

现在我在 Firebase 中找不到类似的东西。首先,我认为不要再切换这些愚蠢的值了。但现在我不再在我的 TestFlight 应用程序中收到任何推送通知。

在我的调试控制台中,当我在设备上进行调试时,一个输出是这样的:

<FIRInstanceID/WARNING> APNS Environment in profile: development

这有利于本地调试,但在 TestFlight 中不需要。 (我不知道 TestFlight 应用程序是否会发生这种情况,因为我没有它们的控制台。)

所以我的问题是:有人知道我是否可以在 Firebase 中手动更改此沙盒选项吗?

谢谢,

西蒙

【问题讨论】:

    标签: ios firebase google-cloud-messaging firebase-cloud-messaging


    【解决方案1】:

    我通过将以下代码添加到项目中解决了这个问题。

    通过 TestFlight 安装应用程序时将使用 FIRInstanceIDAPNSTokenType.Sandbox,
    和 FIRInstanceIDAPNSTokenType.Prod,当您的应用在 App Store 上线时。

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

    【讨论】:

      【解决方案2】:

      我按照提供的文档进行操作,但遇到了同样的问题,然后我尝试了快速启动应用程序,它成功了。关键似乎是在获取令牌后添加连接到 FCM 的逻辑,设置文档中缺少此步骤。完成此操作后,它可以在 TestFlight 之外的我的开发设备上运行,而无需任何其他特殊的沙箱开关。

      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]
      

      【讨论】:

        【解决方案3】:

        注意安全,请在下面使用:

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

        不要不必要地将沙盒令牌设置为 prod 类型,反之亦然。

        【讨论】:

          【解决方案4】:

          这是关于setAPNSToken() 函数的。添加设备令牌时,您需要将FIRInstanceIDAPNSTokenType 设置为Prod。我为此使用了swift,我使用的代码是这样的:

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

          此外,如果您只想删除警告,您可以使用生产配置文件。

          【讨论】:

            猜你喜欢
            • 2016-10-09
            • 2019-06-12
            • 1970-01-01
            • 2023-03-10
            • 2020-03-15
            • 2017-02-07
            • 1970-01-01
            • 1970-01-01
            • 2019-12-30
            相关资源
            最近更新 更多