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:
| Action | Details |
|---|---|
| Request location token | Call CLLocationManager.startMonitoringLocationPushes(completion:) within the ARTPushRegistererDelegate.didActivateAblyPush(:) delegate method. This delegate method is the callback for ARTRealtime.push.activate(). |
| Register with Ably | Call 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 result | Use the ARTPushRegistererDelegate.didUpdateAblyPush: callback, which indicates whether the token was successfully saved. |
Related types
ARTDeviceDetails
A DeviceDetails is a type encapsulating attributes of a device registered for push notifications.
Properties
| Property | Description | Type |
|---|---|---|
| id | Unique identifier for the device generated by the device itself | String |
| clientId | Optional trusted client identifier for the device | String |
| formFactor | Form factor of the push device. Must be one of phone, tablet, desktop, tv, watch, car or embedded | String |
| metadata | Optional metadata object for this device. The metadata for a device may only be set by clients with push-admin privileges | Object |
| platform | Platform of the push device. Must be one of ios or android | String |
| deviceSecret | Secret value for the device | String |
| push.recipient | Push recipient details for this device. See the REST API push publish documentation for more details | Object |
| push.state | The current state of the push device being either Active, Failing or Failed | String |
| push.errorReason | When the device's state is failing or failed, this attribute contains the reason for the most recent failure | ErrorInfo |
LocalDevice
An extension of DeviceDetails. In addition to the properties of DeviceDetails, it includes the following:
Properties
| Property | Description | Type |
|---|---|---|
| deviceIdentityToken | A unique identity token for the device | String |
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
| Parameter | Description | Type |
|---|---|---|
| deviceId | A deviceId to filter by | String |
| clientId | A clientId to filter by | String |
| deviceClientId | A client ID associated with a device to filter by | String |
| callback | Called with a ARTPaginatedResult object containing PushChannelSubscription objects or an error | Callback |
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
| Property | Description | Type |
|---|---|---|
| channel | The channel that this push notification subscription is associated with | String |
| deviceId | The device with this identifier is linked to this channel subscription. When present, clientId is never present | String |
| clientId | Devices with this client identifier are included in this channel subscription. When present, deviceId is never present | String |
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
| Parameter | Description | Type |
|---|---|---|
| channel | Channel name linked to this push channel subscription | String |
| deviceId | The device with this identifier will be linked with this push channel subscription | String |
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
| Parameter | Description | Type |
|---|---|---|
| channel | Channel name linked to this push channel subscription | String |
| clientId | Devices with this client identifier are included in the new push channel subscription | String |
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
| Property | Description | Type |
|---|---|---|
| items | Contains 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
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())")
}
}