awesome_notifications_wns

Windows push notifications for awesome_notifications, through the Windows Push Notification Services (WNS).
WNS is Windows's own push service: your server posts to the app's channel, and Windows delivers it. This package gets the channel through the Windows App SDK (which works for unpackaged apps, as Flutter's are), and shows each push through the awesome_notifications engine: same channels, layouts, buttons, images and events as a local notification.
Key Features:
- The WNS channel of your Azure app, and its renewals, in a handler.
- Server-driven notifications: a push carrying an awesome payload is shown by the core.
- Raw data: any other push reaches your app as text (and JSON, when it is).
- Packaged apps (MSIX) are started by Windows for a push; the push that started the app is delivered first.
๐ฆ The awesome_notifications family
Every package of the family, and where each one runs:
| Package | Push | Android | iOS | macOS | Windows | Linux | Web |
|---|---|---|---|---|---|---|---|
awesome_notifications (the core) |
๐ | โ | โ | โ | ๐งช | ๐งช | ๐งช |
awesome_notifications_schedules |
๐ | โ | โ | โ | ๐งช | ๐งช | โ |
awesome_notifications_localizations |
๐ | โ | โ | โ | ๐งช | ๐งช | ๐งช |
awesome_notifications_fcm |
โ | โ | โ | โ | โ | โ | ๐งช |
awesome_notifications_wns (this package) |
โ | โ | โ | โ | ๐งช | โ | โ |
awesome_notifications_unifiedpush |
โ | ๐งช | โ | โ | โ | ๐งช | โ |
awesome_notifications_onesignal |
โ | ๐งช | ๐งช | โ | โ | โ | โ |
awesome_notifications_braze |
โ | ๐งช | ๐งช | โ | โ | โ | โ |
awesome_notifications_airship |
โ | ๐งช | ๐งช | โ | โ | โ | โ |
awesome_notifications_chat |
๐ | โ | โ | โ | ๐งช | ๐งช | ๐งช |
awesome_notifications_media |
โ | โ | โ | โ | ๐งช | ๐งช | ๐งช |
awesome_notifications_calls |
๐ | โ | โ | ๐งช | ๐งช | ๐งช | ๐งช |
awesome_notifications_live_activities |
๐ | โ | โ | ๐งช | ๐งช | ๐งช | ๐งช |
awesome_notifications_foreground_service |
โ | โ | โ | โ | โ | โ | โ |
โ supported ยท ๐ works with push ยท ๐งช preview (implemented, still being validated) ยท โณ planned ยท โ not available on this platform
๐ฒ Installation
dependencies:
awesome_notifications: ^1.0.0-dev.10
awesome_notifications_wns: ^1.0.0-dev.10
1. Register your app in Microsoft Entra ID (Azure)
WNS for unpackaged desktop apps identifies the app by an Azure app registration:
- In the Azure portal, open Microsoft Entra ID โ App registrations โ New registration. Any name; supported account types: Accounts in this organizational directory only.
- Copy its Application (client) ID (a GUID): the app gives it to
initialize. - In Certificates & secrets, create a client secret. Only your server uses it (with the client ID and your tenant ID) to get WNS access tokens. Never put it in the app.
A packaged app (MSIX) also maps its Package Family Name to that Azure app, as described in Microsoft's quickstart.
2. The Windows App Runtime
The package is built with the Windows App SDK 2.7 (downloaded from nuget.org by the build, nothing to add to your project). At run time, an unpackaged app needs the Windows App Runtime 2 on the machine: Windows offers to install it the first time when it is missing. Ship its installer with your app's installer to spare your users that step.
๐ Usage
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:awesome_notifications_wns/awesome_notifications_wns.dart';
@pragma('vm:entry-point')
Future<void> onChannel(WnsChannel channel) async {
// Send channel.uri to your server; renew it before channel.expiration.
}
@pragma('vm:entry-point')
Future<void> onRawData(WnsRawData data) async {
print('Data push: ${data.payload}');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await AwesomeNotifications().initialize(null, [
NotificationChannel(
channelKey: 'push',
channelName: 'Push notifications',
channelDescription: 'Notifications from our server',
),
]);
await AwesomeNotificationsWns().initialize(
azureAppId: '00000000-0000-0000-0000-000000000000', // your Application (client) ID
onChannel: onChannel,
onRawData: onRawData,
);
runApp(const MyApp());
}
initialize answers false where WNS isn't available (not Windows, or no Windows App
Runtime); requestChannel() asks for the channel again (Windows answers the current one
while it is valid).
The payload
A push whose body is a JSON object with a content section is shown as a notification of
the core, with the same fields as createNotification (and the same payload
awesome_notifications_fcm takes, with its sections as objects or as JSON strings):
{
"content": {
"id": 10,
"channelKey": "push",
"title": "Your order is on its way",
"body": "It arrives in about <b>20 minutes</b>.",
"largeIcon": "https://example.com/courier.png",
"payload": { "orderId": "1234" }
},
"actionButtons": [
{ "key": "TRACK", "label": "Track" },
{ "key": "LATER", "label": "Later", "actionType": "SilentAction" }
]
}
The core then shows it exactly as a local notification: channels, layouts, buttons, images,
the four events, and the add-ons (localizations, chat, โฆ) when the payload carries their
sections. Anything else (another JSON object, plain text, or a payload with
"isSilentData": true) is the app's own data and reaches the data handler.
Sending a push from your server
- Get an access token from Microsoft Entra ID with your app's client credentials:
curl -X POST "https://login.microsoftonline.com/<TENANT_ID>/oauth2/v2.0/token" \
-d "grant_type=client_credentials" \
-d "client_id=<APPLICATION_CLIENT_ID>" \
-d "client_secret=<CLIENT_SECRET>" \
-d "scope=https://wns.windows.com/.default"
- Post the payload to the channel URI as a raw notification:
curl -X POST "<CHANNEL_URI>" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-H "Content-Type: application/octet-stream" \
-H "X-WNS-Type: wns/raw" \
--data '{"content":{"id":10,"channelKey":"push","title":"Hello from WNS"}}'
The app must post raw notifications (wns/raw): the core then builds the toast, with every
feature of awesome_notifications. To try it, run the example with your Azure app id:
flutter run -d windows --dart-define=WNS_AZURE_APP_ID=<GUID>, then copy the channel URI it
shows.
๐ Idempotency
Push services deliver at least once: the same message may arrive twice. This package is
idempotent: a push already received creates no notification, emits no
onNotificationCreated / onNotificationDisplayed event again, and reaches the silent data
handler once.
Set an idempotencyKey at the top level of the payload: a text that identifies the event,
the same in every resend of it and different for every other event.
{ "idempotencyKey": "order-1234-shipped", "content": { "id": 1234, "channelKey": "orders", "title": "Your order has shipped" } }
- Without it: every push counts: WNS gives the app no message id.
- Memory: the last 100 keys, kept across app restarts (a file in
%LOCALAPPDATA%\<your app>). - Across services: the keys are kept per push package: send each device's events through one service.
- With the notification
id: a notification created with theidof one already on screen replaces it, so a repeated push can never pile up on screen; theidempotencyKeyalso keeps the events and the silent data from repeating. Give your pushes both.
๐งฉ With the other push packages
This package covers Windows. To reach every platform, add the other push packages to the same app:
| Package | Platforms | Push service | What your server keeps for each device |
|---|---|---|---|
awesome_notifications_fcm |
Android, iOS, macOS, Web | Firebase Cloud Messaging (APNs on Apple platforms) | the FCM token |
awesome_notifications_wns |
Windows | Windows Push Notification Services | the channel URI |
awesome_notifications_unifiedpush |
Android, Linux | UnifiedPush (the distributor the user installed) | a Web Push subscription |
- They share nothing. Each package listens to its own push service, so adding one doesn't
change how another receives. Where a package's service doesn't exist it answers
"unavailable" (
false, an empty token,null) instead of throwing: the same code initializes all of them. - The same payload. Every push package reads the same awesome payload (
content,actionButtons,schedule,localizations,conversation, โฆ): your server builds a push once. FCM data values must be strings (each section as a JSON string, or dot-flattened keys); WNS and UnifiedPush take the JSON object itself. - The same events. A push of any service creates the same events as a local notification.
createdSourceisFirebasefor FCM pushes; WNS and UnifiedPush pushes reportLocalfor now. - One service per device. Your server keeps, for each device, the service it uses and its address, and sends each event through that service only. The idempotency keys are kept per package: an event sent through two services to one device is shown twice.
๐ฑ Platform support
| Feature | Windows |
|---|---|
| Channel URI and its renewals | ๐งช |
| Displayed push (awesome payload) | ๐งช |
| Raw data, app running | ๐งช |
| Push that starts the app | ๐งช packaged apps (MSIX) only |
| Push while the app is closed | ๐งช packaged apps (MSIX) only |
โ supported ยท ๐งช preview (implemented, still being validated) ยท โณ planned ยท โ not available on this platform
- Unpackaged apps (a plain
flutter build windows) receive pushes while they run. For Windows to start the app for a push, it needs package identity: package it as MSIX. - WNS badge and tile notifications are not supported by the Windows App SDK's push for desktop apps; the toast the core builds from the payload covers them.
- On every other platform the calls are safe:
initializeanswersfalse, the channel isnull.
๐ง How it works
| Layer | Role |
|---|---|
| Dart | The channel handler, and the routing of each push: a notification for the core, or raw data. |
| Windows | The Windows App SDK's PushNotificationManager: the bootstrapper loads the Windows App Runtime (unpackaged apps), the channel is created for the Azure app id, and each push (or the one that started a packaged app) is handed to Dart, kept until Dart listens. |
The build downloads Microsoft.WindowsAppSDK.Foundation and Microsoft.Windows.CppWinRT
from nuget.org and generates the C++/WinRT projection once per build folder;
Microsoft.WindowsAppRuntime.Bootstrap.dll ships next to the app.
๐ See also
- awesome_notifications: the notifications this package shows.
- Push notifications in the Windows App SDK (Microsoft).
Libraries
- awesome_notifications_wns
- Windows push notifications for awesome_notifications, through the Windows Push Notification Services (WNS) of the Windows App SDK.