Notifications
Your app can access data about custom notifications sent to the participant from the MyDataHelps platform. See Data Model Overview for more information about notifications.
Query Notifications
Construct a NotificationHistoryQuery
to specify filtering criteria, and execute the query using ParticipantSession.queryNotifications
. This query returns a NotificationHistoryPage
containing NotificationHistoryModel
values.
Consult the programming guide for best practices on working with queries and paged results in MyDataHelpsKit.
SDK Reference
Examples
The content
property of NotificationHistoryModel
is an enum with varying associated values appropriate for each type of notification that is supported. Use Swift pattern matching to display relevant information for each notification type:
let criteria = NotificationHistoryQuery(statusCode: .succeeded)
let result = try await session.queryNotifications(criteria)
for item in result.notifications {
switch item.content {
case let .email(content):
let subject = content?.subject ?? "(No subject)"
print("Email on \(item.sentDate): \(subject)")
case let .push(content):
let title = content?.title ?? "(No title)"
print("Push notification on \(item.sentDate): \(title)")
case let .sms(content):
let body = content?.body ?? "(Unknown)"
print("SMS notification on \(item.sentDate): \(body)")
}
}
When working with specific notification types and statuses, you can avoid switch statements and make assumptions about some optional properties in the notification data:
let criteria = NotificationHistoryQuery(type: .sms, statusCode: .succeeded)
let result = try await session.queryNotifications(criteria).notifications
if let notification = result.first,
case let .sms(.some(content)) = notification.content,
let body = content.body {
print("Your latest message from \(notification.sentDate.formatted()): \(body)")
} else {
print("No recent messages.")
}
In the MyDataHelpsKit example app, see: