Sending Push Notification in Drupal with your plugin

It is difficult to implement even simple features in Drupal. Luckily Drupal is one of the CMS which have the largest number of plugins (modules). For implementing push notification feature, there are some good plugins available. We are selecting one plugin from the module gallery and will try to implement the push notification feature for our custom developed module.

Drupal’s “Push Notifications” Module

The module “Push Notifications” will help us to do the push notifications and device registrations for us. You can find and download the module at Drupal website. The module supports:

  1. Apple Push Notification Service (APNS)
  2. Google Cloud Messaging (GCM) and
  3. Cloud 2 Device Messaging (C2DM).

 How to Implement Push Notification Module?

Login to your admin panel and install your module. Follow the steps given below:

  1. Login to your admin panel.
  2. Go to your admin page: http://yoursite.com/?q=admin/modules/install
  3. In the field “Install from a URL”, use the following URL: http://ftp.drupal.org/files/projects/push_notifications-7.x-1.1.tar.gz (you can find the latest version from the module page).
  4. Click on “Install” button.
  5. If everything works fine, you will get a message like “Installation was completed successfully.”
  6. Click on the “Enable newly added modules” link to go to the modules listing page.
  7. Go down to the page to see the “Push Notifications” plugins under “Others” category.
  8. Check the enabled column and save the configurations. Your push notification module is enabled now.
  9. Add the certificates and key in the page: Configuration > Web Serivces > Push Notifications > Configuration. For APNS certificate, click on “Generate new certificate string” button, rename your certificates to the displayed names and upload to the sites/all/modules/push_notifications/certificates folder.
  10. After saving your settings, you can test messages to the registered devices from “Send Push” tab.

Configure your module to add the dependency

Since your module is going to use push_notifications module, you have to add dependency for the module. The push_notifications module itself is dependent to the  services, so your module is dependent to both services and push_notifications. After adding dependency, your module info file will look like:

Send Push Notifications from your custom module

Now you can use the push_notifications module to send messages from your custom module. The below example will send a message to the node user when someone comments on the node.

There are many other useful functions in the push_notifications module. You can find more details about the module in our article: Drupal Push Notifications Module’s Functions List (API Manuel).

Drupal Push Notifications Module’s Functions List (API Manuel)

The goal of this article is to give developers’ awareness about the functions included in the push notification module that can be used for your Drupal module development. There are around 20 functions available in the push notification module from which I am selecting 6 functions that can be used for your module to send notifications to APNS and GCM/C2DN services:

push_notifications_send_message

This function is the most valuable, which can be used to send push notification messages to selected users. The function sends messages to both Android and iPhone devices. There are two parameters for this message: the first parameter accepts the user IDs as an array; the second parameter accepts the message.

Usage in your module:

push_notifications_get_tokens

This function will get all registered tokens either for Android or iPhone devices. The function accepts three parameters: the device type id, a particular language based registered tokens, the return format – as raw token value array or as token object.

Note: The default type ids’ are: 0 – Apple; 1 – Android.

You have to use only the first parameter if you are not using any specific languages for your applications. Usage:

push_notification_get_user_tokens

This function will help you to get all devices registered under a particular user. The function accepts User ID and returns the device tokens for both Apple and Android devices.

Usage in your module:

push_notifications_apns_send_message

The function will send messages to the tokens given through the Apple Push Notification Service. The function accepts two parameters: Array of IOS tokens; The payload array to send message.

Make sure that you pass your payload in the correct array format. A valid format of the payload with custom data is given below:

push_notifications_gcm_send_message

This function will send a message to the Android devices through the Google Cloud Messaging service by accepting Android device tokens. The parameters are same as the previous method. The payload format is slightly different from the APNS function.

A valid payload format is:

push_notifications_c2dm_send_message

The application of this function is same as the previous function. The only difference is that this function will use the Android Cloud to Device Messaging service instead of GCM. The two parameters and its formats are the same like the previous function.

Note: For implementing push notification in your custom module, please read the article: Sending Push Notification from your Drupal Module.