Notification and OneSignal in Flutter

Hi guys,
I would like to find a way to send general and individual push notifications to IOS, android and web devices. Through a bit of searching I came across with OneSignal.
The problem is, it works on IOS/Android, but not on web. So you have to use some javascript.

Do you know a better solution?

I’ve tried it so far, but I keep getting the same error. Does anyone have an idea how to proceed?

My code:

void main() {
WidgetsFlutterBinding.ensureInitialized();
Future.delayed(Duration(seconds: 1), () {});
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(home: OneSignalTestScreen());
}}

class OneSignalTestScreen extends StatefulWidget {
const OneSignalTestScreen({super.key});
@override
State createState() => _OneSignalTestScreenState();
}

class _OneSignalTestScreenState extends State {
get http => null;

@override
void initState() {
super.initState();
initializeScreen();}

var uuid = Uuid().v4();
final String _oneSignalId = “xxxxxxxxxxxxxxxxxxxxxxxxxxx”;
OneSignalHelper oneSignalHelper = OneSignalHelper();

Future initializeScreen() async {
if (!kIsWeb) {
await oneSignalHelper.initNative(
oneSignalId: _oneSignalId,
oneSignalUserId: uuid,
addClickListenerCallback: () {});
} else {}}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(),
body: Container(),
);}
}

index.html:

test_notify

The error I get is this one:

WorkerMessenger.js:118
`[Worker Messenger] [Page → SW] Could not get ServiceWorkerRegistration to postMessage!

I have seen that several people have this error, but no solution has helped me…

Thank you for your help :slight_smile:

My recommendation is to use firebase_messaging. I have bad experience using any other push notification service (OneSignal, Pusher). They seem atractive, but only bring problems: OneSignal is specially hard to setup and I recently had to migrate a project from Pusher because it stopped worked on one platform (Android I think) besides missing some critical features.

Why Firebase?

It’s well maintained by Google, is mandatory for Android (the others use Firebase under the hood) and it also integrates with Apple Push Notification Service (APNS). It says it works for web as well so you should be good there (I’ve never tried).

Good luck on your project!

Thank you. I tried it and it works very well for ios, android and web. Thank you. The topic is done.