Firebase云消息传递未第二次注册

时间:2016-08-03 07:24:14

标签: ios firebase apple-push-notifications firebase-cloud-messaging

我正在使用Firebase Cloud Messaging在我的iOS应用程序中向我发送推送通知。我正在收听AppDelegate中kFIRInstanceIDTokenRefreshNotification的通知。一旦应用程序委托在application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)中获取设备令牌,我的kFIRInstanceIDTokenRefreshNotification选择器方法就会被调用。

我的问题是,一旦用户退出然后再次登录,这次在application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData)收到设备令牌后,kFIRInstanceIDTokenRefreshNotification的选择器方法未被调用。

作为观察点,选择器方法在第一次调用后不会被调用(即使用户没有注销)。

我在下面附上我要求的代码。如果代码的任何其他部分是必要的,请告诉我。请问有人可以解释一下我在哪里出错了。

class AppDelegate: UIResponder {
      func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(tokenRefreshNotification(_:)), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
         return true
      }

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

      func tokenRefreshNotification(notification: NSNotification) {
           print("Called only first time")
      }

     func loginFirebaseWithFacebookAccessToken(accessToken: String) {
          let credential = FIRFacebookAuthProvider.credentialWithAccessToken(accessToken)
          if let user = FIRAuth.auth()?.currentUser {
               user.linkWithCredential(credential) { (user, error) in
                   print("Sign in done")
               }
          } else {
               FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
                   print("Sign in done")
               }
           }
     }

    func signOutFromFirebase() {
        do {
            try FIRAuth.auth()?.signOut()
        }
        catch {
        }
    }
}

2 个答案:

答案 0 :(得分:1)

actually i tried this lines, works for me:-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

   [[FIRInstanceID instanceID] deleteIDWithHandler:^(NSError *  _Nullable error) {
       // Add observer for InstanceID token refresh callback.
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil]; 
   }];
    return true;
 }

答案 1 :(得分:0)

你试图强行退出吗? 尝试! FIRAUTH.auth()!signOut()