Skip to content

Device Registration API

In order to receive push notifications from Backendless, a device must be registered using the API documented below. Device registration may optionally include a list of messaging channels and/or an expiration date/time when the registration should be canceled.

Important

Push notifications can be published using either API or Backendless Console. When a push notification is published, it goes through a * messaging channel* . Channels provide a way to establish a level of filtering - devices registered with a channel will receive notifications published to the channel. However, there are other way to narrow down push notification delivery. For example, a push notification may be sent to a specific device or a group of devices.

If no channels are specified in the device registration call, Backendless registers the device with the default channel. If the device registration call references a non-existing channel, Backendless creates the channel and registers the device with it. Registration expiration is a point in time (expressed as a timestamp) when the device registration must expire. Backendless removes the device registration at the specified time and the device no longer receives published push notifications.

In order to receive push notifications on a mobile device, application running on the device must register with Backendless using the API call below:

Blocking API

// registers device with FCM and with the Backendless "default" messaging channel
public void registerDevice()

// registers device with FCM and with the specified Backendless messaging channels
public void registerDevice( List<String> channels )

// registers device with FCM and with the specified Backendless messaging channels. 
// assigns the expiration timestamp for the Backendless registration
public void registerDevice( List<String> channels, Date expiration )

Non-Blocking API

// registers device with FCM and with the Backendless "default" messaging channel
public void registerDevice( AsyncCallback<DeviceRegistrationResult> callback )

// registers device with FCM and with the specified Backendless messaging channels
public void registerDevice( List<String> channel, 
                            AsyncCallback<DeviceRegistrationResult> callback )

// registers device with FCM and with the specified Backendless messaging channels. 
// assigns the expiration timestamp for the Backendless registration
public void registerDevice( List<String> channels, 
                            Date expiration,
                            AsyncCallback<DeviceRegistrationResult> callback )

where:

Argument                Description
channels A collection of messaging channels the device registration will be associated with. Messages published to the channels will be delivered to the associated devices. For the methods without the argument, Backendless registers the device with the default channel;
expiration  A timestamp when the device registration should expire.

Return value (for the non-blocking methods)

The callback's generic type for the non-blocking methods is the DeviceRegistrationResult class. The object of that type is delivered to the handleResponse method of the callback and contains the following properties:

Argument                Description
deviceToken Device token received by the device as a result of registering with FCM. The value is a String object and is available with the getDeviceToken() method.
channelRegistrations A Java Map between Backendless channel name (key name in the map) and the registration ID of the device for that channel (value assigned to the key). When a device is registered with multiple Backendless messaging channels, this map will contain a mapping for each channel. To obtain channelRegistrations map use the getChannelRegistrations() method.

Errors

The following errors may occur during the message publishing API call. See the Error Handling section for details on how to retrieve the error code when the server returns an error:

Error Code
Description
5004
Invalid expiration date. The expiration date must be after the current time.
8000
Property value exceeds the length limit. Error message should contain additional details about the violating property.

Example

// do not forget to call Backendless.initApp in the app initialization section 
List<String> channels = new ArrayList<String>();
channels.add( "default" );
Backendless.Messaging.registerDevice(channels, new AsyncCallback<DeviceRegistrationResult>() {
    @Override
    public void handleResponse(DeviceRegistrationResult response) {
        Toast.makeText( context, "Device registered!",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        Toast.makeText( context, "Error registering " + fault.getMessage(),
                Toast.LENGTH_LONG).show();
    }
});

Codeless Reference

push_device_registration_api_1

where:

Argument                Description
channel name A list of messaging channels the device registration will be associated with. Messages published to the channels will be delivered to the associated devices. If no values are passed to this parameter, then Backendless registers the device with the default channel.
return result Returns an object with the registrationId parameter, containing a Map between Backendless channel name (key name in the map) and the registration ID of the device for that channel (value assigned to the key). When a device is registered with multiple Backendless messaging channels, this map will contain a mapping for each channel.

The example below registers the device with the "ads" and "discount" channels:

push_device_registration_api_2