IPhone Push Notification using Ruby
Push Notification is one of the challenging concept in all smart phones. Especially when it comes to Iphone push, it is quite long process. I recently had a chance to try Push notification in Iphone.
The most difficult task is to create SSL certificate and Provisional certificate. Let’s see how to create it and make use of it.
1. Create Certificate Signing Request (CSR)
Open Keychain Access application in your mac and generate CSR as follows
* Request a Certificate from authority as shown
* It will open a dialog, follow the process to create the certificate. The certificate with certSigningRequest extension will be created.
Save the certificate to disk for further use
2. Create SSL Certificate
We can create our ssl certificate in provisioning portal of IOS dev center. The steps to create SSL certificate
* Create a New App ID by clicking on APP IDs in the left menu as shown
* To enable Push Notification, check the Enable Push Notification Service box and clicking Configure button will open an dialog box as shown
* The Dialog will prompt for CSR certificate. Upload the certificate just created in above steps and follow the window to create APN certificate
* Opening it in Keychain Access, you will able to see Certificate, Private,Public Key as shown
3. Create Provisioning Profile
Now we have to create a Provisioning profile for newly created SSL certificate. The steps are
* Create Provisioning profile by clicking Provisioning in left menu of IOS provisioning portal as shown
* By filling the necessary details, Development Provisioning profile with mobileprovision will be generated as shown
* Download,open it in Xcode and install the profile for your iphone/ipad.
4. Export APN certificate to .pem format
We need to export the generated .cer certificate to .pem format as follows
* Open KeyAccess, Select your generated certificate alone with private key and export it to p12 format as shown
* In terminal, use the following code to export it to pem format
openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes
Make a note .pem certificate file, we will use it in ruby
5. Device Token
Each Device has token associated with it. We need to get the Device Token in order to make communication with that device. In rhodes it is much easier to retrieve the device token. The steps to get device token using rhodes
* Create a rhodes app
rhodes app app_name
* Enable push in build.yml
capablities: - push
* In terminal, type the following to run your rhodes app in Iphone through Xcode
rake swtich_app rake build:iphone:setup_xcode_project
* Connect you device to Mac. Open rrhorunner.xcodeproj in Xcode and run the rhodes app in your device.
* Running you app in device will prompt you to allow for notification as follows
* Clicking Allow will get the device id from device. You can get the device id from the log window of xcode or through code as
System.get('device_id') # => D12f5sad7673-h543
6. Send Messages using ruby gem
Once you have all the necessary things needed to communicate with IOS device, we can use apns gem exclusively for IOS device. The steps are
* First install apns gem as
gem install apns
* Configure apns with your generated pem file, device token as
APNS.host = 'gateway.sandbox.push.apple.com' APNS.pem = '/path/to/your/cert.pem' APNS.port = 2195 device_token = 'your_device_token'
* Send notification to your ios device as
APNS.send_notification(device_token, 'Hello iPhone!')
The entire code looks like
require 'apns' APNS.host = 'gateway.sandbox.push.apple.com' APNS.pem = '/path/to/your/cert.pem' APNS.port = 2195 device_token = 'your_device_token' APNS.send_notification(device_token, 'Hello iPhone!')
Note : Mobile Provision will not be installed in your xcode until it has the proper private key associated with it.
We will get an message in IOS device. I hope this blog would be useful to send push notification in Iphone using ruby gem. Any suggestion or comments are most welcome