Push Notifications

RevTwo on iOS uses push notifications to create a Skype-like workflow when a support tech calls you.  When the app is not in the foreground, the user is prompted with a notification.  Clicking on that notification will open your app and allow the user to either accept or decline the call.

Notifications are delivered to your app via the Apple Push Notification System, or APNS.  For this to work, there are a few things you will need to do on your app.

NOTE: if push notifications are not working, make sure the APNS certificate uses the same bundle ID as your app and the app’s provisioning profile. If any of these do not match, no notifications will come through APNS.

APNS Certificate

The certificate is generated for your app through Apple and uploaded to your app’s model.

  1. Log in to the Apple member center at developer.apple.com and click on the Certificates, Identifiers, & Profiles section.Apple-Home
  2. Select Identifiers under iOS AppsApple-Identifiers
  3. This will show you a list of your app ids.
    • If your app’s bundle id is NOT in the list, create a new app ID without wildcard by clicking the + sign above the list.  Check the “Push Notifications” option under App Services.  Hit create, and it should now show up in the list.
    • If your app’s bundle id is in the list, click on it to see if “Push Notifications” are enabled.  If they are not, click on edit, and add them to your bundle id.
  4. Click on “Certificates”>”All”apple-certiicates-all
  5. Click on the + sign to create a new Certificate.  Select VoIP  Services and click continue.apple-newcert
  6. Select your app id from the drop down & click continue.
  7. Follow the instructions to generate a CSR file:
    • Open the Keychain Access App on your computer.
    • In the Keychain Access app menu, select Keychain Access > Certificate Assistant > Request a Certificate from a Certificate Authority. RequestCertificate
    • Enter your email and common name.  Leave CA email blank and select the “Saved to disk” radio button.  Hit Continue.certificateassistant
    • Save the file CertificateSigningRequest.certSigningRequest to your computer in a new folder.
  8. Back in the apple developer portal, hit continue and upload the file CertificateSigningRequest.certSigningRequest that you just created.  Hit Generate.
  9. Download the certificate voip_services.cer file to your computer and put it in the same folder as the other file.
  10. Open your voip_services.cer file in the Keychain Access app.
  11. Expand your VoIP Services cert by clicking on the arrow.  Right click on your private key and select the export option.exportprivatekey
  12. Leave the name of the file Certificates.p12 and when prompted, leave the password blank.  Hit ok, and in a second popup window, you will be prompted for your keychain password.  This is the password for your computer.  Type it in and hit allow.  Save this file into the same folder you put the other two files in.
  13. Open up the terminal app and navigate to the directory your Certificates.p12 file is in.

    New to terminal?  Just type in “cd ” then drag the folder your file is in, into the window.  Hit enter and you will be in the directory.

  14. From this directory, type the following commands into terminal:
    openssl x509 -in voip_services.cer -inform der -out MyCert.pem
    openssl pkcs12 -nocerts -out MyKey.pem -in Certificates.p12

    When you hit enter, you will be prompted to input a password.  Hit enter without typing anything.
    Next, you will be prompted for a PEM pass phrase.  Enter a passcode of your choice that is at least 4 characters long.  You will need this passcode again in a few minutes.

    cat MyCert.pem MyKey.pem > final.pem
  15. After executing these commands, you will have a file called final.pem
    Open this file in the text editor of your choice (such as Text Wrangler or Sublime Text) and copy the file’s contents.
  16. Sign in to revtwo.com and go to your app’s model page.  Hit Edit Model.
  17. Scroll down to Push notifications and check enable push notifications.
  18. Paste the contents of final.pem into the certificate / private key field.
  19. Type the PEM pass phrase into the pass phrase field.
  20. Save your app model.
  21. On the model detail page, hit the “test connection” button.  You should get ‘success’

Provisioning Profile

To use push notifications in a production or testflight environment, you need a provisioning profile to add to your app.

  1. Log in to the Apple member center at developer.apple.com and click on the Certificates, Identifiers, & Profiles section.Apple-Home
  2. Select Provisioning Profiles under iOS Appsprovisioningprofiles
  3. Hit the + sign to create a new provisioning profile.
  4. Select the App Store profile type under Distribution & hit continue.provisioning1
  5. Select the app id for your app & hit continue.
  6. Select the certificates you want to include & hit continue.
  7. Name the profile something descriptive such as appnameAppStore, so you know what profile it is later.  Hit Generate.
  8. Download the profile you just created.
  9. Double click the profile file to add it to Xcode.
  10. In your Xcode project, navigate to build settings > code signing > provisioning profile.  Expand the drop down and add your new provisioning profile to release by selecting its name from the list.addprovisioningprofile

Add notification capabilities in your app

  1. In Xcode, go to you app’s capabilities and enable:
    1. Push Notifications
    2. Background Modes (select the two modes below):
      1. Audio, AirPlay, and Picture in Picture
      2. Voice Over IP
  2. In your AppDelegate.h file, add the

    <PushKit/PushKit.h> import and add PKPushRegistryDelegate to your app delegate’s interfaces.  Your file should look something like this:

    #import &amp;lt;UIKit/UIKit.h&amp;gt;
    #import &amp;lt;PushKit/PushKit.h&amp;gt;
    @interface AppDelegate : UIResponder &amp;lt;UIApplicationDelegate, PKPushRegistryDelegate&amp;gt;
    @property (strong, nonatomic) UIWindow *window;
    @end
    
  3. In your AppDelegate.m file, add the following lines to your application

    didFinishLaunchingWithOptions method:

    // Create a push registry object
    PKPushRegistry * voipRegistry = [[PKPushRegistry alloc] initWithQueue: dispatch_get_main_queue()];
    // Set the registry's delegate to self
    voipRegistry.delegate = self;
    // Set the push type to VoIP
    voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
    
  4. Add the following delegate functions at the bottom of your AppDelegate.m file:
    /*
     * PKPushRegistryDelegate
     */
    // Handle updated push credentials
    - (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials: (PKPushCredentials *)credentials forType:(NSString *)type {
      //update the apple device token
      R2UpdatePushCredentials(credentials.token);
    }
    
    
    
    // Handle incoming pushes
    - (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
      // Process the received push
      R2ReceivedNotification(payload.dictionaryPayload);
    }
    

Permissions

Push notifications require you to ask your user for permission to show them notifications.

Open up the file for the first view in your app, and add the following to the view controller’s viewDidAppear function:

-(void)viewDidAppear:(BOOL)animated{
 UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
 [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
}