2014-10-26 59 views
1

我正在使用解析API在移動iOS設備之間發送推送通知。但是,當應用程序在後臺時,通知不會到達。我檢查解析SDK iOS - 推送通知僅在應用程序處於前景時纔會顯示

的事情:我的應用程序的

-settings收到通知設置

- 更換背景模式設置

required background modes is set

-app功能設置

enter image description here

- 登記的通知是Facebook驗證完成後啓動應用程序時

- (void) authenticateUserWithFacebook { 
NSArray *permissionsArray = @[@"user_relationships",@"user_friends",@"read_friendlists",@"publish_actions"]; 

[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) { 

    if (!user) { 
     if (!error) { 
      NSLog(@"Uh oh. The user cancelled the Facebook login."); 
     } else { 
      NSLog(@"Uh oh. An error occurred: %@", error); 
      [[PFFacebookUtils session] closeAndClearTokenInformation]; 
      [[PFFacebookUtils session] close]; 
      [[FBSession activeSession] closeAndClearTokenInformation]; 
      [[FBSession activeSession] close]; 
      [FBSession setActiveSession:nil]; 
      [PFUser logOut]; 
      [FBSession renewSystemCredentials:^(ACAccountCredentialRenewResult result, NSError *error) { 
       NSLog(@"%@",error); 
       [self authenticateUserWithFacebook]; 
      }]; 
     } 
    } else if (user.isNew) { 
     NSLog(@"User with facebook signed up and logged in!"); 

     [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
      [[PFUser currentUser] setObject:[result valueForKey:@"id"] forKey:kFbIdKey]; 
      [[PFUser currentUser] setObject:[result valueForKey:@"name"] forKey:@"name"]; 
      [[PFUser currentUser] saveInBackground]; 
     }]; 
     [self loadUsersFacebookFriends]; 
     if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){ 
      [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
     } else { 
      [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
      (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 
     } 

    } else { 
     NSLog(@"User with facebook logged in!"); 

     [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 
      [[PFUser currentUser] setObject:[result valueForKey:@"id"] forKey:kFbIdKey]; 
      [[PFUser currentUser] setObject:[result valueForKey:@"name"] forKey:@"name"]; 
      [[PFUser currentUser] saveInBackground]; 
     }]; 

     if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]){ 
      [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil]]; 
     } else{ 
      [[UIApplication sharedApplication] registerForRemoteNotificationTypes: 
      (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; 

     } 
    } 
}]; 
} 

- 註冊委託方法被解僱

- (void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
    [currentInstallation setDeviceTokenFromData:deviceToken]; 
    [currentInstallation setObject:[[PFUser currentUser] objectForKey:@"fbId"] forKey:kOwnerKey]; 
    [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
if (error) 
    NSLog(@"error %@",error); 
}];   
} 

- (void) application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { 
     [application registerForRemoteNotifications]; 
} 

-notification委託方法只調用時在前臺

- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 

- 我也嘗試使用新的配置文件。相同的結果。

- 我從未見過應用程序想要發送通知的彈出式通知。

+0

這隻ios8只是? – 2014-10-26 15:05:45

+0

以及我省略了iOS7部分,只是爲了讓代碼少一點。 – 2014-10-26 15:09:34

+0

plz至少要添加你實際調用registerUserSettings的地方 – 2014-10-26 15:10:59

回答

0

我終於找到問題所在。 我以錯誤的方式發送推送通知。 這隻適用於應用程序在前臺。

[PFPush sendPushDataToQueryInBackground:pushQuery withData:gameData]; 

這是我需要使用的代碼。

[PFPush sendPushMessageToQueryInBackground:pushQuery withMessage:@"test"]; 
+0

當應用程序在後臺時,來自推送JSON有效負載的數據用於顯示警報,徽章等。如果有效負載沒有提示鍵,則不會顯示推送。同樣的情況也發生在您身上 – 2014-12-17 06:24:50

相關問題