Skip to main content

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

Android and Linux push notifications for awesome_notifications through UnifiedPush: the user's distributor, encrypted Web Push messages.

awesome_notifications_unifiedpush #

Awesome Notifications

Flutter Discord

Open Source Love pub package Full tests workflow codecov badge

Android and Linux push notifications for awesome_notifications, through UnifiedPush.

UnifiedPush is an open standard where a distributor, chosen and installed by the user (such as ntfy on Android, or KUnifiedPush on KDE Plasma), keeps the connection to a push server and wakes the apps for their messages. Linux has no push service of its own; on Android it is the push service of devices without Google Play services, and of users who prefer not to depend on Google. Your server sends standard Web Push messages, encrypted for keys only this app holds; this package decrypts them and shows each push through the awesome_notifications engine: same channels, layouts, buttons, images and events as a local notification.


Key Features: #

  • No vendor account: any UnifiedPush server (ntfy, NextPush, Gotify, Autopush) and any distributor of the UnifiedPush spec (Android apps, D-Bus services on Linux).
  • Web Push subscription (endpoint, p256dh, auth): any Web Push library of your server sends to it.
  • End-to-end encryption (RFC 8291): the distributor and the server never read the content.
  • Server-driven notifications: a push carrying an awesome payload is shown by the core; any other reaches your app as data.
  • App activation: the distributor starts the app for a push, also when it is closed.

๐Ÿ“ฆ 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 โœ… โŒ โŒ โŒ ๐Ÿงช โŒ โŒ
awesome_notifications_unifiedpush (this package) โœ… ๐Ÿงช โŒ โŒ โŒ ๐Ÿงช โŒ
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_unifiedpush: ^1.0.0-dev.10

What the user needs #

A distributor. On Android, an app such as ntfy (from F-Droid or Google Play) or another distributor. On KDE Plasma, KUnifiedPush (in System Settings โ†’ Push Notifications, configured with a server such as ntfy.sh or the user's own). Other desktops can install it too; any distributor that implements the UnifiedPush D-Bus spec works.

Android #

Nothing to set up: the package declares its service, and the official UnifiedPush connector (a dependency of the package) finds the distributors, keeps the Web Push keys and decrypts the messages. The distributor starts the app for each push, also when it is closed: a notification is shown with no Flutter engine, and data waits for the app's next initialize.

Linux: your app's id #

The app is reached on the D-Bus by its application id: the APPLICATION_ID of linux/CMakeLists.txt (for example com.example.myapp). Keep it stable: the distributor knows your app by it.

Linux: starting the app for a push #

To receive pushes while the app is closed, the D-Bus must be able to start it: install a service file named after the application id. Packages usually ship it:

# /usr/share/dbus-1/services/com.example.myapp.service
# (Flatpak: /app/share/dbus-1/services/)
[D-BUS Service]
Name=com.example.myapp
Exec=/usr/bin/myapp

For an app that isn't packaged, AwesomeNotificationsUnifiedPush().enableAppActivation() writes it to the user's folder (~/.local/share/dbus-1/services/), pointing to the running executable. The app then starts as usual, and the push waits until it initializes this package.


๐Ÿš€ Usage #

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:awesome_notifications_unifiedpush/awesome_notifications_unifiedpush.dart';

@pragma('vm:entry-point')
Future<void> onNewEndpoint(UnifiedPushEndpoint endpoint) async {
  // Send endpoint.toSubscription() to your server: a standard Web Push subscription.
}

@pragma('vm:entry-point')
Future<void> onMessage(UnifiedPushMessage message) async {
  print('Data push: ${message.text}');
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AwesomeNotifications().initialize(null, [
    NotificationChannel(
      channelKey: 'push',
      channelName: 'Push notifications',
      channelDescription: 'Notifications from our server',
    ),
  ]);

  final push = AwesomeNotificationsUnifiedPush();
  await push.initialize(
    onNewEndpoint: onNewEndpoint,
    onMessage: onMessage,
    vapid: '<your server VAPID public key>', // when your distributor requires one
  );

  // At every start: the distributor answers the same endpoint.
  final result = await push.register();
  if (result.failure == UnifiedPushFailure.chooseDistributor) {
    // Several are installed: let the user pick one of push.distributors(),
    // then push.register(distributor: chosen).
  }
  runApp(const MyApp());
}

register picks the distributor already chosen, the user's default distributor (Android) or the UNIFIEDPUSH_DISTRIBUTOR environment variable (Linux), or the only one installed; with none, or several, it says so (UnifiedPushFailure.noDistributor / chooseDistributor). distributors() lists package names on Android and bus names on Linux. unregister() stops the pushes. The Web Push keys are created once and kept by the app, readable only by it (the connector's database on Android; the app's data folder, readable only by the user, on Linux).

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 #

Send to the subscription like to a browser, with any Web Push library (aes128gcm encoding). For example, with web-push in Node:

const webpush = require('web-push');
webpush.setVapidDetails('mailto:you@example.com', VAPID_PUBLIC_KEY, VAPID_PRIVATE_KEY);

await webpush.sendNotification(
  subscription, // the app's endpoint.toSubscription()
  JSON.stringify({ content: { id: 10, channelKey: 'push', title: 'Hello from UnifiedPush' } }),
  { contentEncoding: 'aes128gcm', urgency: 'high' },
);

The message must fit in 4096 bytes once encrypted. To try it, run the example (on Android, or flutter run -d linux), register with your distributor and copy the subscription 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: on Linux, the message id the distributor gives is the key, when it gives one. It recognizes a message the distributor delivered twice; your own key also covers an event your server sends again as a new message. On Android the distributors give the app no message id: without your key, every push counts.
  • Memory: the last 100 keys, kept across app restarts (Android: the package's shared preferences; Linux: its state file).
  • Encrypted: the key is read after the message is decrypted, so it stays private between your server and the 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 Android and Linux. 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.
  • On Android, with awesome_notifications_fcm: both can be active, each receiving its own service's pushes. Register in UnifiedPush only the devices that should use it (for example, those with a distributor and without Google Play services).
  • Not with UnifiedPush's embedded-fcm-distributor library (an FCM fallback for UnifiedPush on Android): it declares its own Firebase messaging service, which conflicts with awesome_notifications_fcm.

๐Ÿ“ฑ Platform support #

Feature Android Linux
Distributors, registration and endpoint ๐Ÿงช ๐Ÿงช
Displayed push (awesome payload) ๐Ÿงช ๐Ÿงช
Data push, app running ๐Ÿงช ๐Ÿงช
Displayed push while the app is closed ๐Ÿงช (no Flutter engine) ๐Ÿงช (D-Bus activation)
Data push while the app is closed ๐Ÿงช (at the next initialize) ๐Ÿงช (D-Bus activation)

โœ… supported ยท ๐Ÿงช preview (implemented, still being validated) ยท โŒ not available on this platform

  • Android: a data push that arrives while the app is closed is kept (the last 100) and reaches onMessage when the app initializes this package again; no background isolate is started for it.
  • Linux: the app is started by D-Bus activation as a regular launch (with its window).

๐Ÿ”ง How it works #

Layer Role
Dart The same API on both platforms. On Linux: the distributor's choice, the token (UUIDv4), the Web Push keys (P-256 and the auth secret), the decryption (RFC 8291 / 8188) and the routing of each push: a notification for the core, or data.
Android The official connector (org.unifiedpush.android:connector): the distributors, the registration, the Web Push keys and the decryption. The package's PushService routes each push in every app state: a notification through the core's native engine, or data for Dart (kept while Dart isn't listening).
Linux The connector of the UnifiedPush D-Bus spec (DBUS_0.3.0): the app owns its application id on the session bus and exports org.unifiedpush.Connector2; it calls the distributor's org.unifiedpush.Distributor2. The calls that arrive before Dart listens wait for it.

๐Ÿ”— See also #

0
likes
150
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

Android and Linux push notifications for awesome_notifications through UnifiedPush: the user's distributor, encrypted Web Push messages.

License

Apache-2.0 (license)

Dependencies

awesome_notifications, flutter, pointycastle

More

Packages that depend on awesome_notifications_unifiedpush

Packages that implement awesome_notifications_unifiedpush