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.
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
10
11
// Add the Ably SDK into your build.gradle dependencies and ensure there's a reference to Maven in the repositories section:
dependencies {
// io.ably.pubsub:device requires Android API level 24 (minSdk 24) or later
implementation 'io.ably.pubsub:device:2.0.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.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
4
5
6
7
8
9
10
11
import { ReactNativePush } from '@ably/pubsub-device/react-native-push';
import AsyncStorage from '@react-native-async-storage/async-storage';
import messaging from '@react-native-firebase/messaging';
const Push = ReactNativePush.create({
storage: AsyncStorage,
requestToken: async () => ({ transportType: 'fcm', token: await messaging().getToken() }),
});
const pubsubDeviceClient = getPubSubRealtimeClient({ plugins: { Push } });
await pubsubDeviceClient.push.activate();Update the token when the platform rotates it
The push platform can rotate the device's token at any time. On React Native your application receives the rotation, so forward the new token to Ably with push.updateToken() to keep the device registration deliverable:
1
2
3
messaging().onTokenRefresh(async (token) => {
await pubsubDeviceClient.push.updateToken({ transportType: 'fcm', token });
});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:
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.