This is the quickest way to integrate into Onesignal. Once it's running you should then do it properly by asking for notification permission via an In-App Message, and pass your OneSignal App ID another way so that it's not visible in the code.
You can then trigger notifications Client-side via the onesignal API, or server-side from your backend via cloud/edge functions to the OneSignal API.
Quick and dirty implementation is...
You'll need 3 custom actions to:
Initialise OneSignal
Login user into OneSignal
Log out user from OneSignal
Initialise - Link install instance to OneSignal via custom action triggered by setting it as a Final Action in main.dart
Action Code:
import 'package:onesignal_flutter/onesignal_flutter.dart';
Future onesignalInitialise() async {
final String oneSignalAppId = "YOUR_ONESIGNAL_APP_ID"; // Final actions can't have params, so find a way to pass this ID into the custom action
//Remove this method to stop OneSignal Debugging
OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
OneSignal.initialize(oneSignalAppId);
// The promptForPushNotificationsWithUserResponse function will show the iOS or Android push notification prompt.
//We recommend removing the following code and instead using an In-App Message to prompt for notification
//permission
OneSignal.Notifications.requestPermission(true);
}
Add the OneSignal package dependency to the pubspec in the custom action:
Set this initialisation custom action as final action in main.dart:
Log In - subscribe your user to the OneSignal Push Notification channel by logging the user into OneSignal and referring to them by your backend DB's UUID by setting the OneSignal External ID as your DB's UUID.
Action Code:
import 'package:onesignal_flutter/onesignal_flutter.dart';
Future onesignalLogin(String pUserId) async {
OneSignal.login(pUserId);
}
Trigger action in action flow, log into your app and after that pass your authenticated UserID as the parameter to the custom action:
Log Out - Deregister the user (their OneSignal External ID) from further push notifications by logging them out of OneSignal when they log out your app.
Action Code:
import 'package:onesignal_flutter/onesignal_flutter.dart';
Future onesignalLogout() async {
OneSignal.logout();
}
Run custom code just before the action to log the user out your app.
And that's it. You now have the most basic of integrations into OneSignal. No need to enable Push Notifications in FF for this. That's only for Firebase Messaging (FCM)
Be aware that OneSignals Flutter SDK does not support Web notifications. So, if you test run in the browser it's going to throw all kinds of errors relating to missing plugins.
To properly test, you need to deploy your app to a phone and test there. OneSignal Initialisation won't work without the code running on an actual device as it pulls a unique fingerprint from the device to uniquely identify and register it.