2014-12-04 68 views
0

好吧,我有一個應用程序,我想要實現推送通知。問題是我想用用戶名來幫助確定他們發送通知的設備。但是當它在代理中被調用時,用戶還沒有登錄,所以應用程序崩潰。代碼是用這種方法從外部應用程序代理調用推送通知註冊

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
    currentInstallation[@"installationUser"] = [[PFUser currentUser]username]; 
    // here we add a column to the installation table and store the current user’s ID 
    // this way we can target specific users later 

    // while we’re at it, this is a good place to reset our app’s badge count 
    // you have to do this locally as well as on the parse server by updating 
    // the PFInstallation object 
    if (currentInstallation.badge != 0) { 
     currentInstallation.badge = 0; 
     [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
      if (error) { 
       // Handle error here with an alert… 
      } 
      else { 
       // only update locally if the remote update succeeded so they always match 
       [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 
       NSLog(@"updated badge"); 
      } 
     }]; 
    } 
} 

所以我需要這個不被調用,如果用戶還沒有登錄,但然後我希望它被調用每次。我對如何做到這一點感到困惑。如果您有任何想法,請告知我,謝謝!

+0

查看我的回答供審閱。最好把它放在viewDidAppear方法中的主視圖控制器中。確保設置了所有的通知標記並正確調用了方法。以下是其他人查看的文檔:https://parse.com/tutorials/login-and-signup-views – soulshined 2014-12-05 00:29:07

+0

我回答了您的問題嗎?如果是這樣,請讓我相信正確的答案 – soulshined 2014-12-05 18:41:53

回答

0

你通常不會把這種在applicationDidBecomeActive我看到你的文檔閱讀了一點點,但它真正的輕鬆:

if (![PFUser currentUser]) { // No user logged in : this is all you need because in applicationDidRegistreForRemoteNotificationsWithDeviceToken should have set the user or installation ID already 
//Enter data 
} else { 
// User logged in 
} 

注意:這是最好的在你的主視圖中使用控制器viewDidAppear方法。查看他們的文檔以備將來的指導:https://parse.com/tutorials/login-and-signup-views