Configure and activate devices
To send push notifications with Ably, you must first configure the device using its platform-specific notification service (FCM or APNs) and integrate it with Ably's infrastructure. You can then activate the push notifications service, either directly or via a server.
Configure devices
Configuration is the first step in setting up push notifications with Ably. This step requires setting up the necessary infrastructure on the device's operating system or platform, as well as on the Ably platform.
Configure FCM for Android devices
- Go to the Firebase Console.
- Click add project and follow the steps to create and manage service account keys.
- Download your service account JSON file.
- In your Ably dashboard, navigate to the Notifications tab under your app settings.
- Go to push notifications setup, click configure push.
- Add your service account JSON file to the setting up Firebase cloud messaging section.
Install and set up your Ably SDK
Use the following code to install and set up your Ably SDK:
1
2
3
4
5
6
7
8
9
// Add the Ably SDK into your build.gradle dependencies and ensure there's a reference to Maven in the repositories section:
dependencies {
implementation 'io.ably:ably-android:1.2.0'
}
repositories {
mavenCentral()
}Activate devices
To receive push notifications using Ably, each device must first register with it's platform-specific push notification service (FCM or APNs). Ably's SDK facilitates this registration process and provides a unified API to manage it across different platforms.
Activate directly
Activating a device for push notifications is typically done directly on the device itself. Use the push.activate() method to activate push notifications from a device. The following steps describe the direct activation process:
- Authenticate the Ably client.
- Generate a unique identifier for the device and saves it locally.
- Activate push notifications for the device with the underlying OS, obtaining a unique identifier like a registration token for FCM or a device token for APNs.
- Register the device with Ably, providing its unique identifier (
deviceId), platform details, and push recipient information. - Store the device's identity token from Ably's response locally for authentication in subsequent updates.
Note that once activated, the device remains registered even after the application is closed until the push.deactivate() method is called. Calling activate again has no effect if the device is already activated.
The following diagram demonstrates the direct activation process:
The following example initializes an instance of the Ably Realtime service and then activates the push notifications feature for that instance:
1
2
3
AblyRealtime ably = getAblyRealtime();
ably.setAndroidContext(context);
ably.push.activate();Test your push notification activation
- Use the Ably dashboard or API to send a test push notification to a registered device.
- Ensure your application correctly receives and handles the push notification.
Activate via server
In specific scenarios, especially where strict control over device capabilities is essential, managing the activation of devices for push notifications through the server is useful. This approach separates the registration with the device's platform-specific push notification service, which still happens on the device. Meanwhile, your server manages the device registration with Ably. This setup provides a centralized and efficient process for controlling devices.
The following steps explain the server assisted activation process:
- The device initiates the activation process by registering with its platform service to obtain a unique device identifier, such as a registration token for FCM or device token for APNs.
- Instead of registering itself with Ably, the device sends its platform-specific identifier to your server. Your server then uses this identifier to register the device with Ably using Ably's Push Admin API.
- The server stores and manages the device tokens. It is responsible for updating these tokens in Ably's system whenever they change, which can be triggered by the device sending a new token to the server following the platform service's renewal.
The following diagram demonstrates the process for activating devices from a server:
Activate Android devices via server
Your application must interact with the Ably SDK to configure server-side registration for push notifications, utilizing the LocalBroadcastManager for communication.
Ensure that the useCustomRegisterer parameter is set to true when calling push.activate() or push.deactivate().
1
2
ably.push.activate(context, true);
ably.push.deactivate(context, true);Upon activation or deactivation, the Ably SDK broadcasts signals indicating whether to register or deregister the device from your server. It uses io.ably.broadcast.PUSH_REGISTER_DEVICE for registration and io.ably.broadcast.PUSH_DEREGISTER_DEVICE for de-registration. To handle these broadcasts, implement a listener in your app's AndroidManifest.xml and respond by sending back PUSH_DEVICE_REGISTERED or PUSH_DEVICE_DEREGISTERED as appropriate.
The following examples set up the server-side activation:
1
2
3
4
5
6
<receiver android:name=".MyAblyBroadcastReceiver">
<intent-filter>
<action android:name="io.ably.broadcast.PUSH_REGISTER_DEVICE" />
<action android:name="io.ably.broadcast.PUSH_DEREGISTER_DEVICE" />
<intent-filter>
<receiver>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class MyAblyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
AblyRealtime ably = getAblyRealtime();
String action = intent.getAction();
if ("io.ably.broadcast.PUSH_REGISTER_DEVICE".equals(action)) {
Intent response = new Intent("io.ably.broadcast.PUSH_DEVICE_REGISTERED");
boolean isNew = intent.getBooleanExtra("isNew", false);
LocalDevice device;
try {
device = ably.device();
} catch (AblyException e) {
IntentUtils.addErrorInfo(response, e.errorInfo);
return;
}
try {
String deviceIdentityToken = registerThroughYourServer(device, isNew);
response.putExtra("deviceIdentityToken", deviceIdentityToken);
} catch (AblyException e) {
IntentUtils.addErrorInfo(response, e.errorInfo);
}
LocalBroadcastManager.getInstance(context.getApplicationContext()).sendBroadcast(response);
} else if ("io.ably.broadcast.PUSH_DEREGISTER_DEVICE".equals(action)) {
Intent response = new Intent("io.ably.broadcast.PUSH_DEVICE_DEREGISTERED");
LocalDevice device;
try {
device = ably.device();
} catch (AblyException e) {
IntentUtils.addErrorInfo(response, e.errorInfo);
return;
}
try {
deregisterThroughYourServer(device.id);
} catch (AblyException e) {
IntentUtils.addErrorInfo(response, e.errorInfo);
}
LocalBroadcastManager.getInstance(context.getApplicationContext()).sendBroadcast(response);
}
}
}Test your push notification activation
- Use the Ably dashboard or API to send a test push notification to a registered device.
- Ensure your application correctly receives and handles the push notification.
Device activation lifecycle
When the push.activate() and push.deactivate() methods are called, the device registers with FCM or APNs. In addition, whenever there is an update to the push token (registration token for FCM or device token for APNs), the Ably SDK attempts to update this token on Ably's servers.
| Event | Method |
|---|---|
| Handle push activation | io.ably.broadcast.PUSH_ACTIVATE in a broadcast intent |
| Handle push deactivation | io.ably.broadcast.PUSH_DEACTIVATE in a broadcast intent |
| Handle push registration failure | io.ably.broadcast.PUSH_UPDATE_FAILED in a broadcast intent |