0
votes

I'am implementing a solution to allow the user to receive push notifications only from the list of users he is following. I read about advanced targeting recipients when pushing messages. How should I setup up my installation object in order to receive posts only from the users I’m following. Should I fetch the list of users he is following and setup the installation object. The issue is, I’m using Parse login view controller and I’ll get the list of users I’am following, only after I login. My second question is if the user will be able to get the push notifications if he is not logged in. My third question, is a new Parse installation object is created every time the app is launched? The requirement is I should see push notification from users I'am following. Please advise.

1

1 Answers

0
votes

I have had similar questions and concerns while implementing my own authentication and push alert system. This is how I handled it.

in AppDelegate.m:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
PFInstallation *currentInstallation = [PFInstallation currentInstallation];

if (authenticated) {
    NSString *user = someUniqueStringForUser;
    [currentInstallation addUniqueObject:user forKey:@"channels"];
}

[currentInstallation setDeviceTokenFromData:deviceToken];
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        NSLog(@"Successful Registration %s", __PRETTY_FUNCTION__);
    } else {
        NSLog(@"Error %@, %s", error, __PRETTY_FUNCTION__);
    }
}];

}

In your login ViewController.m

- (void)authenticatedAndRegisterForPush {
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert)];
    NSString *appUser = uniqueString;
    [currentInstallation addUniqueObject:appUser forKey:@"channels"];
    [currentInstallation saveInBackground];
}

I'm not 100% sure you'll need to add channels again since the didRegister method handles the logic, but I'm doing it and it seems to be working correctly. Then in whatever view controller you have to select people to follow you just call the PFInstallation object, subscribe it to channels, and save.

When you want to send push notifications, you'll send it to all people who are subscribed to those channels. Parse will fail to send a push notification if there is no device token information registered to the installation.

I believe there are some minor changes to remote/push notifications in iOS 8, so if you're building against the iOS SDK there may be minimal changes to the appDelegate methods.

If you want to remove push notifications for a user you'll have to do the opposite of the registration process. Call the installation, remove all channels it'll listen to, and then save.

To my understanding, PFInstallation looks to see if there is an installation instance related to the appBundle. So if you were to log out and log back in, it recognizes the device and installation as the same. I've only had issues with logging out and push notifications only if I didn't clean out the channels correctly. There is also a pain with testing when deleting and re-installing on the same device creating multiple installations. I haven't found a great solution to this particular problem, but if you do please let me know.

Hope that helps.