Push notifications segmentation

Discussion in 'iOS Programming' started by dado1, Nov 23, 2015.

  1. dado1 macrumors member

    Joined:
    Sep 12, 2009
    #1
    We are using PHP to create API for mobile and Pusher to trigger events. We need to send notifications to specific users identified by unique ID.

    Can someone please explain how to do it?
     
  2. AxoNeuron, Nov 23, 2015
    Last edited: Nov 23, 2015

    AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #2
    To generate a unique ID for each user who installs your app you can use this code:

    Code:
    + (NSString *)generateUUID
    {
      CFUUIDRef uuid = CFUUIDCreate(NULL);
      CFStringRef idString = CFUUIDCreateString(NULL, uuid);
      CFRelease(uuid);
      return [(NSString *)idString autorelease];
    }
    
    //add it to the keychain
    - (void) blah
    {
      [SSKeychain setPassword:[self generateUUID] forService:@"your.bundle.identifier" account:@"user"];
    }
    
    From then on it can be retrieved from the keychain using:

    Code:
    - (void) blah
    {
      NSString*uuidString =[SSKeychain passwordForService:@"your.bundle.identifier" account:@"user"];
    }
    
    That UUID will be the same across a user's devices assuming they use iCloud keychain, or if they get a new phone and restore it from a backup it will also stay the same.

    You could also do something more interesting, like generate a random 32 character string with alphanumeric characters, then add a UNIX timestamp (a number representing milliseconds since January 1st 1970) to the end of the string. Then apply an SHA-256 hash to it. Then you will have a totally unique identifier.

    Here's my favorite method using SHA-256 to produce a hashed string:

    Code:
    #include <CommonCrypto/CommonDigest.h>
    
    -(NSString*) sha256:(NSString *)clear{
        const char *s=[clear cStringUsingEncoding:NSASCIIStringEncoding];
        NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
        uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
        CC_SHA256(keyData.bytes, keyData.length, digest);
        NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
        NSString *hash=[out description];
        hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
        hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
        hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];
        return hash;
    }
    
    Personally I deeply dislike PHP and I prefer Node.js for things like this. There are Push notification libraries out there for Node.js that make it incredibly easy.

    Make sure to make it VERY easy to turn off push notifications for your app. Don't just make users go to Settings > Notifications, you could make it easier than that. And don't send too many notifications (try to stay below 1 notification per every 4-5 days), unless you are making a messaging platform or something like that where push notifications will be crucial. After all, apps that send too many push notifications to their users tend to get uninstalled pretty quickly, and users will resent it so greatly they'll often give your app a 1 star review.

    An even more interesting tactic is to use A/B testing. It's pretty easy to implement. Basically you send two (or more) versions of the same push notification to your users, and you can learn what sorts of things will get your users more engaged.
     

Share This Page