Push notifications for Android and iOS with Drupal and Titanium

I recently developed a Drupal site that serves as a backend for a mobile application (that I also built parts of). At an early stage of the project it came clear that the client wanted push notifications implemented.

Drupal was chosen by the client since they already knew the system and was going to create content for the application from the web. They wanted an application that could run on both iOS and Android why we used Titanium Appcelerator. The core of the application was around certain dates and the push was to go out on the day before these dates. Also not all devices should be notified for all dates. The users needed to be able to pick dates themselves. Titanium has it's own service for push notifications which has integration built in. Two other popular services are Urban Airship and Parse. These two have modules available in the Market place. However the services where all to expensive and since I needed to write logic for when the pushes where to go out on the Drupal side I decided to set this up by myself without third party services.

Drupal

I started researching for how to get the push notification functionality up on Drupal and soon found out that of course there is a module for that. The module integrates with the Services module to let devices sign up for pushes and then it integrates with both APNS (Apple Push Notification Service) and GCM (Google Cloud Messaging) to send out pushes to devices. For Android all that is needed is to get an API key for GCM. However for iOS there is bit more work. You will need to enable Push notifications for the application you are building by logging into Apple developer and generate certificates etc. All instructions are in the readme for the module. I had some troubled that got solved with some help from the maintainer.

Test a push

After you have this set up on the Drupal side you can test to send pushes from the admin interface of the module at admin/config/services/push_notifications/message. To test you need to add some devices in the database.

Services

The Push notifications module comes with a Services endpoint for devices to be added to recieve pushes. I created my own endpoint in a custom module since I needed more data to be stored and also more functionality around when to push. In my final solution I did not use a lot of the functionality from the contrib module but had my own endpoint, database table and implemented hook_cron to run push_notifications_apns_send_message().

Titanium

On the client side you first want the application to register for push notifications.

iOS

On iOS the user only gets the popup to enable push notifications for an application once which is good to keep in mind. It does´t matter if you do a full reinstall of the application. When the application starts up you need to implement the method Titanium.Network.registerForPushNotifications() and make a call to your Drupal endpoint.

Android

For Android I found this module on GitHub. I installed the module but ran in to some problems with the settings in tiapp.xml. I made it work with the following settings. [Sender ID from GCM service] 2130837504 com.my-domain-name.applicationname/com.my-domain-name.applicationname.ApplicationnameActivity com.activate.gcm 1. Make sure you set the correct version number. 2. You will need to have a Google account on the device you use for testing. The module comes with installation instructions and code examples that made it easy to use.

Conclusion

Not much code in this tutorial since most of the process is reading documentation and configuration. On the Titanium side you need to check the device OS with Ti.Platform.osname. After that register notifications separately for Android and iOS. The next step is to send your token to Drupal with Titanium.Network.HTTPClient.

I used

Titanium 2.1.3 Drupal 7 Push notifications drupal module 7.x-1.0 GCM Titanium module v0.5