'flutter one signal not displaying notifications
I am trying to use One Signal and flutter to be able to add user notifications to the app, however I have run into a problem. The app receives the notification (according to the console) but does not show the notification in the phone (notification is authorized in phone config).
here is the App.dart code
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
OneSignal.shared.setAppId(
"APP ID",
);
OneSignal.shared.setRequiresUserPrivacyConsent(true);
OneSignal.shared.promptUserForPushNotificationPermission().then((accepted) {
print("Accepted permission: $accepted");
});
OneSignal.shared
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
print('NOTIFICATION OPENED HANDLER CALLED WITH: ${result}');
});
OneSignal.shared.setNotificationWillShowInForegroundHandler(
(OSNotificationReceivedEvent event) {
// Will be called whenever a notification is received in foreground
// Display Notification, pass null param for not displaying the notification
event.complete(event.notification);
});
return MaterialApp(
builder: (context, widget) => ResponsiveWrapper.builder(
BouncingScrollWrapper.builder(context, widget!),
maxWidth: 1200,
minWidth: 450,
defaultScale: true,
breakpoints: [
ResponsiveBreakpoint.resize(450, name: MOBILE),
ResponsiveBreakpoint.autoScale(800, name: TABLET),
ResponsiveBreakpoint.autoScale(1000, name: TABLET),
ResponsiveBreakpoint.resize(1200, name: DESKTOP),
ResponsiveBreakpoint.autoScale(2460, name: "4K"),
],
background: Container(color: Color(0xFFF5F5F5))),
home: SplashScreenView(),
debugShowCheckedModeBanner: false,
);
}
}
And here is the console log after sending the notification from the website
I/WM-WorkerWrapper(28728): Worker result SUCCESS for Work [ id=id, tags={ com.onesignal.OSNotificationWorkManager$NotificationWorker } ]
Solution 1:[1]
Lates Updated method
OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
print('FOREGROUND HANDLER CALLED WITH: ${event}');
/// Display Notification, send null to not display
event.complete(null);
this.setState(() {
_debugLabelString =
"Notification received in foreground notification: \n${event.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
});
});
just move the app in the background or close it and test the push notification. it will show you if everything is correctly set up. But if you want to receive a notification when the app is running & open (foreground mode) you need to manage it manually via the flutter local notifications package.
check this one signal official link this method is mentioned.
This function/callback will be called whenever your application receives a notification from OneSignal when the app is in the foreground.
OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
// a notification has been received
});
above method, you can fire local notification when you received push notification while the app is in the foreground mode.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
