Skip to main content

awesome_notifications_wns 1.0.0-dev.10 copy "awesome_notifications_wns: ^1.0.0-dev.10" to clipboard
awesome_notifications_wns: ^1.0.0-dev.10 copied to clipboard

PlatformWindows

Windows push notifications for awesome_notifications through the Windows Push Notification Services (WNS).

awesome_notifications_wns #

Awesome Notifications

Flutter Discord

Open Source Love pub package Full tests workflow codecov badge

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:

  1. In the Azure portal, open Microsoft Entra ID โ†’ App registrations โ†’ New registration. Any name; supported account types: Accounts in this organizational directory only.
  2. Copy its Application (client) ID (a GUID): the app gives it to initialize.
  3. 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 #

  1. 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"
  1. 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 the id of one already on screen replaces it, so a repeated push can never pile up on screen; the idempotencyKey also 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. createdSource is Firebase for FCM pushes; WNS and UnifiedPush pushes report Local for 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: initialize answers false, the channel is null.

๐Ÿ”ง 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 #

0
likes
150
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Windows push notifications for awesome_notifications through the Windows Push Notification Services (WNS).

License

Apache-2.0 (license)

Dependencies

awesome_notifications, flutter

More

Packages that depend on awesome_notifications_wns

Packages that implement awesome_notifications_wns