Skip to main content

Streams of notifications

When developing an application, you might find that the notifications you want to send to users fall into general groups, based on the type or timing of their content. For example, you might have “pre-trade” and “post-trade” notifications, or ones related to HR, IT, and site operations. In addition, individual users might be interested in some groups but not others, and wish to turn them on and off separately.

Notification streams enable you to categorize notifications and they enable users to toggle streams based on their own preferences, without turning off all notifications from your application as a whole.

User story

Suppose your management has given you the following user story to implement:

As a user, I want to control whether I see notifications from different departments.

Recipe

To assign a stream to a notification item, add a stream option, which is a NotificationStream and consists of these members:

  • id: A unique ID for the stream.
  • appId: A unique ID for the application creating the stream. Use the same ID as in your application's manifest, so that Notification Center can properly match your notifications with your app.
  • displayName: A name for the stream that appears in the Notification Center.

Note: For any specific ID value, if you use a new value for displayName value, the name changes for the new notification and for any previous notifications with the same ID.

Code example

const notification = 
{
"title": "Benefits enrollment begins in two weeks",
"stream":
{
"id": "73241c15-3d15-4c33-8c01-90cf133a473b",
"appId": "6343fb2d-accd-4934-8735-9de204a104fd"
"displayName": "HR Update"
},
"body": "The open enrollment period for company benefits begins two weeks from today.",
"icon": "http://EXAMPLE.COM/examples/notifications/company-T.png",
"buttons":
[
{
"title": "Review benefits",
"type": "button",
"cta": true, //makes the button prominent by coloring it blue
"onClick":
{
"task": "open-benefits-site"
}
},
{
"title": "Ignore",
"type": "button",
}
]
};

create(notification);
NotificationOptions notificationOptions = new NotificationOptions
{
Title = "Benefits enrollment begins in two weeks",
Stream = new NotificationStream
{
Id = "73241c15-3d15-4c33-8c01-90cf133a473b",
AppId = "6343fb2d-accd-4934-8735-9de204a104fd",
DisplayName = "HR Update",
},
Body = "The open enrollment period for company benefits begins two weeks from today.",
Icon = "http://EXAMPLE.COM/examples/notifications/company-T.png",
Buttons = new[]
{
new ButtonOptions
{
Title = "Review benefits",
IsCallToActionButton = true, //makes the button prominent by coloring it blue
OnNotificationButtonClick = new Dictionary<string, object>
{
{
"task",
"https://open-benefits-site"
}
}
},
new ButtonOptions
{
Title = "Ignore",
}
}
};

// The notification ID uniquely identifies the notification and can be any unique string.
String notificationId = Guid.NewGuid().ToString();

await NotificationClient.CreateNotificationAsync(notificationId, notificationOptions);