PlayPortalNotificationClient

This class is responsible for registering for Push Notifications and making requests to the playPORTAL notifications API.
public class PlayPortalNotificationClient

The PlayPortalNotificationClient class simplifies registering your app for Apple Push Notifications and allows you to create and send notifications between users. Notifications can be ephemeral or persisted, in which case you will be able to request those persisted notifications. To use push notifications, you will need to enable it in your app. Take a look at the Enabling the Push Notification Service section from this tutorial for more information.

You will need to enable the "Push Notifications" scope for your app in the developer dashboard before using the notifications API.

Properties

public static let shared: PlayPortalNotificationClient

Singleton instance of PlayPortalNotificationClient. You will access all the available PlayPortalNotificationClient methods through this instance.

Methods

register

public func register(options: [UNAuthorizationOptions] = [.badge, .sound, .alert], deviceToken: Data? = nil, _ completion: ((_ error: Error?) -> Void)?) -> Void

Register your app for Apple Push Notifications. If you are already using push notifications in your app, you can simply pass in your existing device token.

If you pass in a device token but there is no currently authenticated user, an assertion will be raised.

Parameter

Type

Description

options

[UNAuthorizationOptions]

The authorization options for your notifications.

deviceToken

Data?

If your app is already registered for remote notifications, you can pass in your existing device token.

completion

((Error?) -> Void)?

The closure invoked once the push notification request flow finishes.

error

Error?

An error can occur during the push notification authorization flow, or be returned from the notification API.

didRegisterForRemoteNotifications

public func didRegisterForRemoteNotifications(withDeviceToken deviceToken: Data, _ completion: ((_ error: Error?) -> Void)?) -> Void

Handles device token registration. Your AppDelegate will need to implement the application(_:didRegisterForRemoteNotificationsWithDeviceToken:) method, which will then invoke the didRegisterForRemoteNotifications method, passing in the deviceToken argument.

Parameter

Type

Description

withDeviceToken

Data

The device token returned by APN.

completion

((Error?) -> Void)?

The closure invoked when the request finishes.

error

Error?

The error returned for an unsuccessful request.

createNotification

public func createNotification(text: String, receiver: String, persist: Bool = true, _ completion: ((_ error: Error?) -> Void)?) -> Void

Create and send a push notification to another playPORTAL user.

Parameter

Type

Description

text

String

The notification text; this is what would be displayed in the notification alert.

receiver

String

The user id of the playPORTAL user that will receiver the notification.

persist

Bool

Whether the notification should be persisted; if true, the receiver of the notification will be able to retrieve it when requesting their notifications.

completion

((Error?) -> Void)?

The closure invoked when the request finishes.

error

Error?

The error returned for an unsuccessful request.

readNotifications

public func getNotifications(since: Int? = nil, page: Int? = nil, limit: Int? = nil, acknowledged: Bool = true, _ completion: @escaping (_ error: Error?, _ notifications: [PlayPortalNotification]?) -> Void) -> Void

Request all persisted notifications for the current user. Supports pagination through the page and limit parameters.

Parameter

Type

Description

since

Int?

Return notifications after this date; expected in Unix epoch time. If not provided, will return all notifications.

page

Int?

At what page to return notifications, where page length is determined by limit.

limit

Int?

The maximum number of notifications to return.

acknowledged

Bool

Whether or not notifications that have been acknowledged should be returned.

completion

(Error?, [PlayPortalNotification]?) -> Void

The closure invoked when the request finishes.

error

Error?

The error returned for an unsuccessful request.

notifications

[PlayPortalNotification]?

An array of notifications returned for a successful request.

acknowledgeNotification

public func acknowledgeNotification(notificationId: String, _ completion: ((_ error: Error?) -> Void)?) -> Void

Acknowledge that a notification has been seen by the current user.

Parameter

Type

Description

notificationId

String

The id of the notification being acknowledged.

completion

((Error?) -> Void)?

The closure invoked when the request finishes.

error

Error?

The error returned for an unsuccessful request.