Skip to main content

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

OneSignal pushes shown by awesome_notifications: layouts, channels, buttons and events of the core, with the official OneSignal plugin kept for everything else.

awesome_notifications_onesignal #

Awesome Notifications

Flutter Discord

Open Source Love pub package Full tests workflow codecov badge

OneSignal pushes shown by awesome_notifications.

This package is a bridge, not a replacement. Keep the official onesignal_flutter plugin for everything OneSignal offers: the subscription, the user, tags, in-app messages and the dashboard. This package makes every OneSignal push be built and shown by the awesome_notifications core, with the same channels, layouts, buttons, images and events as a local notification. OneSignal's own events and statistics keep working for those pushes.


Key Features: #

  • Every push through the core: in foreground, background and with the app closed. On Android this goes through the OneSignal SDK's notification service extension; on iOS, through your app's Notification Service Extension.
  • Your dashboard messages as they are: title, message, big picture, large and small icon, group, buttons and launch URL become an awesome notification.
  • The awesome payload when you need it: content, actionButtons, schedule, localizations, conversation, … in the push's additional data take priority.
  • Both plugins' events: the core's onActionReceived, getInitialAction, created, displayed and dismissed events, and onesignal_flutter's click and foreground listeners, with OneSignal's delivery and open statistics.
  • No push lost: on Android, a push the core can't show (for example, with no channel) is shown by the OneSignal SDK as before.

πŸ“¦ 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 βœ… πŸ§ͺ ❌ ❌ ❌ πŸ§ͺ ❌
awesome_notifications_onesignal (this package) πŸ§ͺ πŸ§ͺ πŸ§ͺ ❌ ❌ ❌ ❌
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


Getting started #

dependencies:
  awesome_notifications: ^1.0.0-dev.10
  awesome_notifications_onesignal: ^1.0.0-dev.10
  onesignal_flutter: ^5.8.1

Set up onesignal_flutter as OneSignal's Flutter guide says (your app id, the iOS capabilities), then initialize the three of them at every start:

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:awesome_notifications_onesignal/awesome_notifications_onesignal.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await AwesomeNotifications().initialize(null, [
    NotificationChannel(
      channelKey: 'push',
      channelName: 'Push notifications',
      channelDescription: 'Messages from our server',
      importance: NotificationImportance.High,
    ),
  ]);
  await AwesomeNotifications().setListeners(
    onActionReceivedMethod: onActionReceived,
  );

  OneSignal.initialize('<your OneSignal app id>');
  await OneSignal.Notifications.requestPermission(true);

  // The channel of a push that names none (kept for the pushes that arrive
  // with the app closed).
  await AwesomeNotificationsOneSignal().initialize(defaultChannelKey: 'push');

  runApp(const MyApp());
}

@pragma('vm:entry-point')
Future<void> onActionReceived(ReceivedAction action) async {
  final oneSignalId = AwesomeNotificationsOneSignal.oneSignalIdOf(action);
  final launchUrl = AwesomeNotificationsOneSignal.launchUrlOf(action);
  // …
}

A channel is required on Android. A push shows on the channel named by content.channelKey in its additional data; otherwise on the push's Android channel (existing_android_channel_id in OneSignal's API) when an awesome channel has that key; otherwise on the defaultChannelKey. Without one, the OneSignal SDK shows the push itself, as without this package.

Android setup #

Nothing to add: the package declares itself as the OneSignal SDK's notification service extension (the com.onesignal.NotificationServiceExtension meta-data). The SDK starts the app's process for each push, also with the app closed, and the core shows the push with no Flutter engine.

To run your own code for each push, extend the package's extension and declare yours instead:

class MyExtension : me.carda.awesome_notifications_onesignal.AwnOneSignalNotificationExtension() {
    override fun onNotificationReceived(event: INotificationReceivedEvent) {
        // your code…
        super.onNotificationReceived(event)
    }
}
<!-- android/app/src/main/AndroidManifest.xml, inside <application>
     (add xmlns:tools="http://schemas.android.com/tools" to <manifest>) -->
<meta-data
    android:name="com.onesignal.NotificationServiceExtension"
    android:value="com.example.app.MyExtension"
    tools:replace="android:value" />

OneSignal's own manifest options keep working for a tap: com.onesignal.suppressLaunchURLs (the launch URL isn't opened) and com.onesignal.NotificationOpened.DEFAULT set to DISABLE (the SDK doesn't bring the app to front; the core still does, for actions that open the app).

iOS: Notification Service Extension (required) #

On iOS, the pushes reach the core through a Notification Service Extension. Without it, iOS shows OneSignal's pushes by itself, as without this package, and the core never sees them. This package's extension replaces OneSignal's: it builds the notification with the core, then runs OneSignal's extension work on it (badge increments and confirmed deliveries).

Follow the steps of awesome_notifications_fcm's Notification Service Extension (creating the target, App Groups, the Embed Foundation Extensions order), with these differences:

  • Swift Package Manager: add the local package ios/Flutter/ephemeral/Packages/.packages/awesome_notifications_onesignal (from pub.dev its name carries the version) and its product AwesomeNotificationsOneSignalExtension to the extension. It brings OneSignal's OneSignalExtension itself.
  • CocoaPods: the extension's target in ios/Podfile:
    target 'NotificationService' do
      use_frameworks!
      pod 'IosAwnCore', :path => '.symlinks/plugins/awesome_notifications/ios'
      pod 'AwesomeNotificationsOneSignalExtension', :path => '.symlinks/plugins/awesome_notifications_onesignal/ios'
    end
    
  • The extension's code:
    import AwesomeNotificationsOneSignalExtension
    
    class NotificationService: AwesomeOneSignalServiceExtension {}
    
  • If you followed OneSignal's guide and already have a OneSignalNotificationServiceExtension target, keep the target and its App Group, and replace its code and its OneSignalXCFramework dependency with the above. One app has one Notification Service Extension.
  • The App Group must be the same in Runner and the extension (OneSignal's guide also asks for one, group.<bundle id>.onesignal, which you may keep alongside).

Every push must carry "mutable-content": 1, or iOS skips the extension: keep "mutable_content": true in OneSignal's API requests.


The payload #

Messages from OneSignal's dashboard or API work as they are:

OneSignal Android (rawPayload) iOS (userInfo) Awesome notification
Notification id custom.i custom.i (or os_data.i) payload onesignalId; the id when none is given
Title title aps.alert.title content.title
Message alert aps.alert.body (or aps.alert) content.body
Big picture / iOS attachment bicon att (the first URL) content.bigPicture, layout BigPicture
Large icon licon (a URL) β€” content.largeIcon
Small icon sicon (a drawable name) β€” content.icon (resource://drawable/…)
Group / thread grp aps.thread-id content.groupKey
Android channel oth_chnl (when an awesome channel has that key) β€” content.channelKey
Buttons custom.a.actionButtons ({id, text}) buttons ({i, n}) actionButtons (key, label)
Launch URL custom.u custom.u payload onesignalLaunchUrl
Additional data custom.a custom.a (or the top level with os_data) content.payload (values as strings)

The id of a notification without its own content.id comes from OneSignal's id, the same on both platforms.

The notification keeps the push's original keys: on iOS in its userInfo, on Android in its extras and in the intent that opens the app.

The awesome sections win. Put content, actionButtons, schedule, localizations, conversation or call in the additional data (as JSON objects, or JSON strings like the FCM payload); they are merged over what the push's own fields give. android and ios sections are merged last, each on its own platform:

{
  "app_id": "<your app id>",
  "include_subscription_ids": ["<subscription id>"],
  "headings": { "en": "Order shipped" },
  "contents": { "en": "Your order 1234 is on its way" },
  "mutable_content": true,
  "data": {
    "content": { "channelKey": "orders", "notificationLayout": "BigPicture",
                 "bigPicture": "https://example.com/box.png", "payload": { "order": "1234" } },
    "actionButtons": [ { "key": "track", "label": "Track" } ]
  }
}

The awesome actionButtons in data replace OneSignal's buttons (on Android, the SDK writes OneSignal's buttons over an actionButtons key in the data: use OneSignal's buttons or the awesome ones, not both).


πŸ‘† Taps and events #

Both plugins see each push. Where only one of them can, the core's events win: onActionReceived and getInitialAction are never changed to let a OneSignal event through.

Event Android iOS
Core: onNotificationCreated / onNotificationDisplayed βœ… every state βœ… every state
Core: onActionReceived, getInitialAction, onDismissActionReceived βœ… βœ…
OneSignal: delivery statistics (receipts, badge increments) βœ… βœ… through the extension
OneSignal: addForegroundWillDisplayListener βœ… can hide or delay the push βœ… fires, but can't hide the push (see below)
OneSignal: addClickListener, open statistics, session influence, launch URL βœ… taps and buttons that open the app βœ… every tap and button
OneSignal: permission and subscription observers βœ… untouched βœ… untouched

How: on Android, the core owns the tap. The core then hands it to the OneSignal SDK through the SDK's own entry for a tap on its notifications, with the original push, so the SDK counts the open and fires its click listener (with the pressed button's key as actionId). On iOS, the OneSignal SDK sees every notification response before the core does and passes it on.

OneSignal events dropped #

  • iOS: the foreground listener can't hide the push. iOS asks the app once whether to show a notification in foreground. The core answers it (with the channel's settings and its onNotificationDisplayed) before OneSignal's listeners run, so a listener's event.preventDefault() has no effect. Hide a push in foreground with the core instead.
  • Android: taps that don't open the app aren't reported to OneSignal. For silent, DismissAction and disabled buttons and for a dismiss, OneSignal's click entry always opens the app, which those actions must not do, so OneSignal's click listener doesn't fire and the open isn't counted.
  • Android: a tap on a notification with autoDismissible: false isn't reported to OneSignal. The core tells its add-ons only of taps that dismiss the notification.
  • Android: the SDK doesn't restore the core's notifications after a reboot or an app update (the core has its own persistence). Only a push the SDK showed itself as a fallback is restored.

Manual test checklist #

For each app state (foreground, background, killed), send a push from OneSignal's dashboard with a launch URL, a button, and additional data, then check:

  • ❌ The core shows the push (its channel, layout and buttons) and emits created and displayed.
  • ❌ Foreground: onesignal_flutter's addForegroundWillDisplayListener fires; on Android, event.preventDefault() hides it and event.notification.display() shows it again.
  • ❌ Tap: onActionReceived fires once (killed: getInitialAction returns the tap).
  • ❌ Tap: onesignal_flutter's addClickListener fires with the push's id and additional data.
  • ❌ Button: both fire, OneSignal's result.actionId is the button's key.
  • ❌ Android, silent button: onActionReceived fires and the app stays closed.
  • ❌ OneSignal's dashboard counts the push as delivered and clicked.
  • ❌ The launch URL opens (unless com.onesignal.suppressLaunchURLs is set).
  • ❌ iOS: the badge increments sent from the dashboard apply.
  • ❌ A push on an unknown channel with no default is shown by OneSignal (Android).

πŸ” Idempotency #

A push OneSignal delivers twice creates one notification:

  • Android: the OneSignal SDK drops a push id it already received before calling the extension.
  • iOS: the extension keeps the last 100 OneSignal ids in the App Group. iOS shows every push that reaches the extension, so a repeated push is shown again (replacing the one with the same id), but its events aren't emitted again.

Give a push its own content.id to replace an earlier notification of the same event.


🧩 With the other push packages #

  • Not with awesome_notifications_fcm nor firebase_messaging. On Android the OneSignal SDK receives its pushes through Firebase's messaging broadcast and stops it there, and only one Firebase messaging service gets each push. On iOS, an app has one Notification Service Extension. Use one push service per app.
  • With awesome_notifications_wns and awesome_notifications_unifiedpush: they share nothing with this package.
  • The other add-ons (chat, localizations, schedules, calls) read their sections from the additional data, as with any push package.

πŸ”€ Migrating from onesignal_flutter alone #

  1. Add the two awesome packages, create your channels, and call AwesomeNotificationsOneSignal().initialize(defaultChannelKey: …) after OneSignal.initialize.
  2. On iOS, replace the code of OneSignal's Notification Service Extension as described above.
  3. Your OneSignal code keeps working: addClickListener and the foreground listener still fire (with the exceptions above). Move the tap handling you want in every app state to the core's onActionReceived: it also receives the taps of local notifications, and runs in background.
  4. If you had your own Android INotificationServiceExtension, extend AwnOneSignalNotificationExtension instead (see Android setup).
  5. To shape a push beyond OneSignal's fields, add the awesome sections to its additional data.

πŸ“± Platform support #

Feature Android iOS
Pushes shown by the core (foreground, background, killed) πŸ§ͺ πŸ§ͺ
OneSignal's fields mapped (title, body, images, buttons, group, launch URL) πŸ§ͺ πŸ§ͺ
Awesome sections in the additional data πŸ§ͺ πŸ§ͺ
OneSignal's click listener and open statistics πŸ§ͺ πŸ§ͺ
OneSignal's foreground listener πŸ§ͺ πŸ§ͺ
OneSignal's extension work (receipts, badge increments) πŸ§ͺ πŸ§ͺ
Fallback to OneSignal's display when the core can't show a push πŸ§ͺ ❌

βœ… supported Β· πŸ§ͺ preview (implemented, still being validated) Β· ⏳ planned Β· ❌ not available on this platform


πŸ”§ How it works #

  • Android: the OneSignal SDK calls AwnOneSignalNotificationExtension for each push (the SDK's INotificationServiceExtension). It maps the push, asks the core to show it, and calls preventDefault(true) so the SDK doesn't show its own. In foreground, the SDK's foreground listeners decide first. The push is kept (the last 50) to hand its taps to the SDK.
  • iOS: AwesomeOneSignalServiceExtension is the core's AwesomeServiceExtension with OneSignal's parser registered. Once the core built the notification, OneSignal's OneSignalExtension runs on it, with OneSignal's data kept and its media and buttons removed (the core already added them).

πŸ”— See also #

0
likes
150
points
--
downloads

Documentation

API reference

Publisher

unverified uploader

OneSignal pushes shown by awesome_notifications: layouts, channels, buttons and events of the core, with the official OneSignal plugin kept for everything else.

License

Apache-2.0 (license)

Dependencies

awesome_notifications, flutter, onesignal_flutter

More

Packages that depend on awesome_notifications_onesignal

Packages that implement awesome_notifications_onesignal