Parse SDK iOS - 推送通知仅在应用程序位于前台时显示

时间:2014-10-26 14:52:10

标签: ios parse-platform apple-push-notifications

我使用Parse api在移动iOS设备之间发送推送通知。但是,当应用在后台时,通知无法到达。

我检查过的事情:

设置我的应用以接收通知

- 设置了所需的背景模式

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 delegate方法

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

- 我也尝试了新的配置文件。同样的结果。

- 我也从未见过该应用想要发送通知的弹出通知。

1 个答案:

答案 0 :(得分:0)

我终于找到了问题所在。 我以错误的方式发送推送通知。 这只适用于应用程序位于前台的情况。

[PFPush sendPushDataToQueryInBackground:pushQuery withData:gameData];

这是我需要使用的代码。

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