awesome_notifications_onesignal

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 productAwesomeNotificationsOneSignalExtensionto the extension. It brings OneSignal'sOneSignalExtensionitself. - 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
OneSignalNotificationServiceExtensiontarget, keep the target and its App Group, and replace its code and itsOneSignalXCFrameworkdependency 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'sevent.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,
DismissActionand 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: falseisn'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'saddForegroundWillDisplayListenerfires; on Android,event.preventDefault()hides it andevent.notification.display()shows it again.Tap:onActionReceivedfires once (killed:getInitialActionreturns the tap).Tap: onesignal_flutter'saddClickListenerfires with the push's id and additional data.Button: both fire, OneSignal'sresult.actionIdis the button's key.Android, silent button:onActionReceivedfires and the app stays closed.OneSignal's dashboard counts the push as delivered and clicked.The launch URL opens (unlesscom.onesignal.suppressLaunchURLsis 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_fcmnorfirebase_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_wnsandawesome_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
- Add the two awesome packages, create your channels, and call
AwesomeNotificationsOneSignal().initialize(defaultChannelKey: β¦)afterOneSignal.initialize. - On iOS, replace the code of OneSignal's Notification Service Extension as described above.
- Your OneSignal code keeps working:
addClickListenerand the foreground listener still fire (with the exceptions above). Move the tap handling you want in every app state to the core'sonActionReceived: it also receives the taps of local notifications, and runs in background. - If you had your own Android
INotificationServiceExtension, extendAwnOneSignalNotificationExtensioninstead (see Android setup). - 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
AwnOneSignalNotificationExtensionfor each push (the SDK'sINotificationServiceExtension). It maps the push, asks the core to show it, and callspreventDefault(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:
AwesomeOneSignalServiceExtensionis the core'sAwesomeServiceExtensionwith OneSignal's parser registered. Once the core built the notification, OneSignal'sOneSignalExtensionruns on it, with OneSignal's data kept and its media and buttons removed (the core already added them).
π See also
- awesome_notifications: channels, layouts, events.
- onesignal_flutter: OneSignal's Flutter SDK.
- awesome_notifications_fcm: push through Firebase Cloud Messaging, with no third-party service.
Libraries
- awesome_notifications_onesignal
- OneSignal pushes shown by awesome_notifications.