Sending Push Notifications Using GCM Service
DreamFactory 2.5 includes support for Google Cloud Messaging (GCM) Service. It is implemented as a native service of DreamFactory. Once you create a GCM service in DreamFactory, you can use it as a proper push notification provider for your application.
DreamFactory GCM service supports sending push notification to a single device using its device token. The service can also act as a push notification provider for your application by supporting device registration by your application. Here is an overview of how DreamFactory works as a push notification service provider.
- In DreamFactory you create an application for your Android app. This application basically represents your Android app in DreamFactory.
- When you create an application in DreamFactory you will get an API Key.
- Store this API Key in your Android app.
- On your Android app, when your user confirms to receive push notification for the app, you make a request to DreamFactory GCM service providing the API Key and the user's device token.
- DreamFactory stores the device token under your API Key.
- When it's time to send push notification, you make a request to the DreamFactory GCM service providing your notification message and the API Key.
- DreamFactory will send the message to all of your registered devices under that API Key.
Contents
- 1 Creating a DreamFactory GCM Service
- 2 API Endpoints
- 3 Example - Sending push notification to all devices registered for your application
- 4 Example - Sending push notification to a single device
- 5 Example - Registering device token
- 6 Example - Replacing existing device token
- 7 Example - Deleting device tokens by API Key
- 8 Example - Listing device tokens by API Key
Creating a DreamFactory GCM Service
You can create a GCM service from the admin console. Log in to the admin console and select the 'Services' tab. Click on the 'Create' button from the left menu. From the Service Type drop down menu select Notification -> GCM Push Notification. Enter all the service configuration information and save the form to create your service. For more details check DreamFactory/Features/Push_Notification_Services.
API Endpoints
Send push notification to all devices registered for your application
POST https://{url}/api/v2/{gcm-service-name}/push?api_key={your-application-api-key}
Request body:
{ "message": "string", "badge": 0, "sound": "string", "action-loc-key": "string", "loc-key": "string", "loc-args": [ "string" ], "custom": [ {} ] }
Note: Only message is required in the body. You can also provide the API Key using the request header X-DreamFactory-Api-Key instead of the api_key
query parameter.
Send push notification to a single device
POST https://{url}/api/v2/{gcm-service-name}/push
Request body:
{ "message": "string", "badge": 0, "sound": "string", "action-loc-key": "string", "loc-key": "string", "loc-args": [ "string" ], "custom": [ {} ], "device_token": "string" }
Note: Only message and device_token is required in the body.
Register device tokens
POST https://{url}/api/v2/{gcm-service-name}/register?api_key={your-application-api-key}
Request body:
{ "device_token": "string" }
Note: You can also provide the API Key using the request header X-DreamFactory-Api-Key instead of the api_key
query parameter.
Replace existing device token
PUT https://{url}/api/v2/{gcm-service-name}/register?api_key={your-application-api-key}
Request body:
{ "old_token": "string", "new_token": "string" }
Note: You can also provide the API Key using the request header X-DreamFactory-Api-Key instead of the api_key
query parameter.
Delete all device tokens
DELETE https://{url}/api/v2/{gcm-service-name}/register?api_key={your-application-api-key}
Note: You can also provide the API Key using the request header X-DreamFactory-Api-Key instead of the api_key
query parameter.
Warning: This will delete all device tokens that are registered under your application.
List all registered device tokens
GET https://{url}/api/v2/{gcm-service-name}/register?api_key={your-application-api-key}
Note: You can also provide the API Key using the request header X-DreamFactory-Api-Key instead of the api_key
query parameter.
Example - Sending push notification to all devices registered for your application
- Service name: apns
- Host: foo.com
- api_key = 123abc
- Request Content-Type: application/json
- Request body:
{ "message": "Hello World!" }
- Request URL:
POST https://foo.com/api/v2/apns/push?api_key=123abc
Example - Sending push notification to a single device
- Service name: apns
- Host: foo.com
- Request Content-Type: application/json
- Request body:
{ "message": "Hello World!", "device_token": "123abc" }
- Request URL:
POST https://foo.com/api/v2/apns/push
Example - Registering device token
- Service name: apns
- Host: foo.com
- api_key = 123abc
- Request Content-Type: application/json
- Request body:
{ "device_token": "123abc" }
- Request URL:
POST https://foo.com/api/v2/apns/register?api_key=123abc
Example - Replacing existing device token
- Service name: apns
- Host: foo.com
- api_key = 123abc
- Request Content-Type: application/json
- Request body:
{ "old_token": "123abc ", "new_token": "456def" }
- Request URL:
PUT https://foo.com/api/v2/apns/register?api_key=123abc
Example - Deleting device tokens by API Key
- Service name: apns
- Host: foo.com
- api_key = 123abc
- Request Content-Type: application/json
- Request URL:
DELETE https://foo.com/api/v2/apns/register?api_key=123abc
Example - Listing device tokens by API Key
- Service name: apns
- Host: foo.com
- api_key = 123abc
- Request Content-Type: application/json
- Request URL:
GET https://foo.com/api/v2/apns/register?api_key=123abc