Push Notifications - Device Activation and Subscription

Push Device object

This object is accessible through client.push and provides to push-compatible devices :

Methods

activate

activate(callback: (ARTErrorInfo?, DeviceDetails?) -> Void)

Register the device for push. When the activation process is completed, Ably will call the didActivateAblyPush(error: ARTErrorInfo?) method from the ARTPushRegistererDelegate.

deactivate

deactivate(deregisterCallback: (ARTErrorInfo?, deviceId: String?) -> Void)

Deregister the device for push. When the deactivation process is completed, Ably will call the didDeactivateAblyPush(error: ARTErrorInfo?) method from the ARTPushRegistererDelegate.

Location push notifications

Starting with iOS 15, Apple supports a power-efficient way to request location through the location push service extension.

The following table explains how to receive location push notifications:

ActionDetails
Request location tokenCall CLLocationManager.startMonitoringLocationPushes(completion:) within the ARTPushRegistererDelegate.didActivateAblyPush(:) delegate method. This delegate method is the callback for ARTRealtime.push.activate().
Register with AblyCall ARTPush.didRegisterForLocationNotifications(withDeviceToken:realtime:) when you receive the location push token. Note the "Location" in the method name to distinguish it from regular push tokens.
Handle resultUse the ARTPushRegistererDelegate.didUpdateAblyPush: callback, which indicates whether the token was successfully saved.

ARTDeviceDetails

A DeviceDetails is a type encapsulating attributes of a device registered for push notifications.

Properties

PropertyDescriptionType
idUnique identifier for the device generated by the device itselfString
clientIdOptional trusted client identifier for the deviceString
formFactorForm factor of the push device. Must be one of phone, tablet, desktop, tv, watch, car or embeddedString
metadataOptional metadata object for this device. The metadata for a device may only be set by clients with push-admin privilegesObject
platformPlatform of the push device. Must be one of ios or androidString
deviceSecretSecret value for the deviceString
push.recipientPush recipient details for this device. See the REST API push publish documentation for more detailsObject
push.stateThe current state of the push device being either Active, Failing or FailedString
push.errorReasonWhen the device's state is failing or failed, this attribute contains the reason for the most recent failureErrorInfo

LocalDevice

An extension of DeviceDetails. In addition to the properties of DeviceDetails, it includes the following:

Properties

PropertyDescriptionType
deviceIdentityTokenA unique identity token for the deviceString

PushChannel

A PushChannel is a property of a RealtimeChannel or RestChannel. It provides push devices the ability to subscribe and unsubscribe to push notifications on channels.

Methods

subscribeDevice

subscribeDevice()

Subscribe your device to the channel's push notifications.

subscribeClient

subscribeClient()

Subscribe all devices associated with your device's clientId to the channel's push notifications.

unsubscribeDevice

unsubscribeDevice()

Unsubscribe your device from the channel's push notifications.

unsubscribeClient

unsubscribeClient()

Unsubscribe all devices associated with your device's clientId from the channel's push notifications.

listSubscriptions

listSubscriptions(deviceId: String?, clientId: String?, deviceClientId: String?, channel: String?, callback: (ARTPaginatedResult<PushChannelSubscription>?, ARTErrorInfo?) -> Void)

Lists push subscriptions on a channel specified by its channel name (channel). These subscriptions can be either be a list of client (clientId) subscriptions, device (deviceId) subscriptions, or if @concatFilters@ is set to @true@, a list of both. This method requires clients to have the Push Admin capability. For more information, see GET main.realtime.ably.net/push/channelSubscriptions Rest API.

Parameters
ParameterDescriptionType
deviceIdA deviceId to filter byString
clientIdA clientId to filter byString
deviceClientIdA client ID associated with a device to filter byString
callbackCalled with a ARTPaginatedResult object containing PushChannelSubscription objects or an errorCallback
Callback result

On success, resultPage contains a PaginatedResult encapsulating an array of PushChannelSubscription objects corresponding to the current page of results. PaginatedResult supports pagination using next() and first() methods.

On failure to retrieve message history, err contains an ErrorInfo object with the failure reason.

ARTPushChannelSubscription

A PushChannelSubscription is a type encapsulating the subscription of a device or group of devices sharing a client identifier to a channel in order to receive push notifications.

Properties

PropertyDescriptionType
channelThe channel that this push notification subscription is associated withString
deviceIdThe device with this identifier is linked to this channel subscription. When present, clientId is never presentString
clientIdDevices with this client identifier are included in this channel subscription. When present, deviceId is never presentString

PushChannelSubscription constructors

PushChannelSubscription.forDevice

PushChannelSubscription.forDevice(String channel, String deviceId) -> PushChannelSubscription

A static factory method to create a PushChannelSubscription object for a channel and single device.

Parameters
ParameterDescriptionType
channelChannel name linked to this push channel subscriptionString
deviceIdThe device with this identifier will be linked with this push channel subscriptionString
Returns

A PushChannelSubscription object

PushChannelSubscription.forClient

PushChannelSubscription.forClient(String channel, String clientId) -> PushChannelSubscription

A static factory method to create a PushChannelSubscription object for a channel and group of devices sharing a client identifier.

Parameters
ParameterDescriptionType
channelChannel name linked to this push channel subscriptionString
clientIdDevices with this client identifier are included in the new push channel subscriptionString
Returns

A PushChannelSubscription object

ARTPaginatedResult

A PaginatedResult is a type that represents a page of results for all message and presence history, stats and REST presence requests. The response from an Ably REST API paginated query is accompanied by metadata that indicates the relative queries available to the PaginatedResult object.

Properties

PropertyDescriptionType
itemsContains the current page of results (for example an Array of Message or PresenceMessage objects for a channel history request)Array <Message, Presence, Stats>

Methods

first

first(callback: (ARTPaginatedResult?, ARTErrorInfo?) -> Void)

Returns a new PaginatedResult for the first page of results.

hasNext

Boolean hasNext()

Returns true if there are more pages available by calling next and returns false if this page is the last page available.

isLast

Boolean isLast()

Returns true if this page is the last page and returns false if there are more pages available by calling next.

next

next(callback: (ARTPaginatedResult?, ARTErrorInfo?) -> Void)

Returns a new PaginatedResult loaded with the next page of results. If there are no further pages, then nil is returned.

Example
Swift

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

channel.history { paginatedResult, error in
    guard let paginatedResult = paginatedResult else {
        print("No results available")
        return
    }
    print("Page 0 item 0: \((paginatedResult.items[0] as! ARTMessage).data)")
    paginatedResult.next { nextPage, error in
        guard let nextPage = nextPage else {
            print("No next page available")
            return
        }
        print("Page 1 item 1: \((nextPage.items[1] as! ARTMessage).data)")
        print("Last page? \(nextPage.isLast())")
    }
}