Skip to main content

In-App Notifications

In-app notifications allow you to communicate timely information to your users directly within your application interface. These notifications can display alerts, announcements, and other important information that enhances user experience and engagement.

Overview

The In-App Notifications API provides endpoints to:

  • Retrieve unseen notifications for users
  • Mark notifications as seen
  • Add target users to notifications

Retrieving Unseen Notifications

Retrieve all unseen notifications for a user with the GET /Notifications/{notificationType}/unseen endpoint.

using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text.Json;

public class NotificationService
{
private readonly HttpClient _client;
private readonly string _baseUrl = "https://apis.lightstone.co.za/lightstone-capabilities-notifications-api/v1";

public NotificationService(string apiKey)
{
_client = new HttpClient();
_client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
}

public async Task<List<Notification>> GetUnseenNotificationsAsync(
string notificationType,
string category = null,
string tenantId = null,
string correlationId = null,
string partyId = null)
{
var requestUrl = $"{_baseUrl}/Notifications/{notificationType}/unseen";

if (!string.IsNullOrEmpty(category))
{
requestUrl += $"?category={category}";
}

if (!string.IsNullOrEmpty(tenantId))
{
_client.DefaultRequestHeaders.Add("x-Authenticated-TenantId", tenantId);
}

if (!string.IsNullOrEmpty(correlationId))
{
_client.DefaultRequestHeaders.Add("correlationid", correlationId);
}

if (!string.IsNullOrEmpty(partyId))
{
_client.DefaultRequestHeaders.Add("x-ls-party-id", partyId);
}

var response = await _client.GetAsync(requestUrl);
response.EnsureSuccessStatusCode();

var content = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<Notification>>(content);
}
}

Marking Notifications as Seen

After a user views a notification, mark it as seen with the POST /Notifications/{name}/seen endpoint.

public class MarkAsSeenRequest
{
public string FlagValue { get; set; }
}

public class NotificationService
{
// ...existing code...

public async Task MarkNotificationAsSeenAsync(
string notificationName,
string flagValue = null,
string tenantId = null,
string correlationId = null,
string partyId = null)
{
var requestUrl = $"{_baseUrl}/Notifications/{notificationName}/seen";

if (!string.IsNullOrEmpty(tenantId))
{
_client.DefaultRequestHeaders.Add("x-Authenticated-TenantId", tenantId);
}

if (!string.IsNullOrEmpty(correlationId))
{
_client.DefaultRequestHeaders.Add("correlationid", correlationId);
}

if (!string.IsNullOrEmpty(partyId))
{
_client.DefaultRequestHeaders.Add("x-ls-party-id", partyId);
}

var payload = new MarkAsSeenRequest
{
FlagValue = flagValue
};

var content = new StringContent(JsonSerializer.Serialize(payload), System.Text.Encoding.UTF8, "application/json");
var response = await _client.PostAsync(requestUrl, content);

response.EnsureSuccessStatusCode();
}
}

Adding Target Users to Notifications

Associate specific users with a notification using the POST /Notifications/{notificationId}/addTargetUsers endpoint.

public class NotificationService
{
// ...existing code...

public async Task AddTargetUsersToNotificationAsync(
Guid notificationId,
List<string> userIds,
string correlationId = null,
string partyId = null)
{
var requestUrl = $"{_baseUrl}/Notifications/{notificationId}/addTargetUsers";

if (!string.IsNullOrEmpty(correlationId))
{
_client.DefaultRequestHeaders.Add("correlationid", correlationId);
}

if (!string.IsNullOrEmpty(partyId))
{
_client.DefaultRequestHeaders.Add("x-ls-party-id", partyId);
}

var content = new StringContent(JsonSerializer.Serialize(userIds), System.Text.Encoding.UTF8, "application/json");
var response = await _client.PostAsync(requestUrl, content);

response.EnsureSuccessStatusCode();
}
}

Notification Object Structure

When retrieving notifications, the API returns a list of notification objects with the following structure:

{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "notification-name",
"tag": "notification-tag",
"heading": "Notification Heading",
"description": "Detailed description of the notification",
"imageUrl": "https://example.com/image.png",
"primaryButtonText": "Action",
"primaryButtonValue": "action-value",
"secondaryButtonText": "Dismiss",
"secondaryButtonValue": "dismiss-value",
"notificationType": 863480000,
"startDate": "2023-01-01T00:00:00Z",
"endDate": "2023-12-31T23:59:59Z",
"closeable": true,
"notificationCategory": 863480000,
"viewMoreLocation": "/details",
"viewMoreText": "View more",
"microFrontEndUrl": "https://example.com/micro-frontend"
}

Best Practices

  • Poll for new notifications: Set up a polling mechanism to check for new notifications periodically.
  • Cache notifications: Store retrieved notifications in a local cache to reduce API calls.
  • Prioritize notifications: Display high-priority notifications more prominently in your UI.
  • Group by category: Organize notifications by category for a better user experience.
  • Handle expired notifications: Check the endDate and don't display expired notifications.

Implementation Checklist

  1. Authenticate with the API using your subscription key
  2. Periodically fetch unseen notifications for the current user
  3. Display notifications in your application UI
  4. Mark notifications as seen when the user interacts with them
  5. Add target users to notifications when creating custom notifications