Future<void> initPlatformState() async {
if (!mounted) return;
OneSignal.Debug.setLogLevel(OSLogLevel.verbose);
OneSignal.Debug.setAlertLevel(OSLogLevel.none);
OneSignal.consentRequired(requireConsent);
// NOTE: Replace with your own app ID from https://www.onesignal.com
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);
OneSignal.LiveActivities.setupDefault();
OneSignal.Notifications.clearAll();
OneSignal.User.pushSubscription.addObserver((state) {
if (kDebugMode) {
debugPrint('OneSignal User 1---${state.current.jsonRepresentation()}');
}
});
OneSignal.User.addObserver((state) {
final userState = state.jsonRepresentation();
if (kDebugMode) {
print('OneSignal user changed: $userState');
}
});
OneSignal.Notifications.addPermissionObserver((state) {
if (kDebugMode) {
print("Has permission $state");
}
});
OneSignal.Notifications.addClickListener((event) {
if (kDebugMode) {
print(
'NOTIFICATION CLICK additionalData: ${event.notification.additionalData}');
}
event.preventDefault();
/// Do async work
/// notification.display() to display after preventing default
event.notification.display();
if (event.notification.additionalData != null) {
final Redirection target = Redirection.fromString(
event.notification.additionalData!['targetPage']);
if (routes.containsKey("/${target.page ?? ""}")) {
final int subRoute = routes["/${target.page ?? ""}"]!.indexWhere(
(element) =>
element.toLowerCase() == (target.subRoute ?? "").toLowerCase(),
);
try {
AppRouter.router.push(
"/${target.page ?? ""}",
extra: subRoute == -1 ? 0 : subRoute,
);
} catch (e) {
throw Exception(e);
}
} else {
AppRouter.router.push(Routes.HOME);
}
}else {
AppRouter.router.push(Routes.HOME);
}
});
OneSignal.Notifications.addForegroundWillDisplayListener((event) {
print(
'NOTIFICATION WILL DISPLAY LISTENER CALLED WITH: ${event.notification.jsonRepresentation()}');
/// Display Notification, preventDefault to not display
event.preventDefault();
/// Do async work
/// notification.display() to display after preventing default
event.notification.display();
if (event.notification.additionalData != null) {
final Redirection target = Redirection.fromString(
event.notification.additionalData!['targetPage']);
if (routes.containsKey("/${target.page ?? ""}")) {
final int subRoute = routes["/${target.page ?? ""}"]!.indexWhere(
(element) =>
element.toLowerCase() == (target.subRoute ?? "").toLowerCase(),
);
try {
AppRouter.router.push(
"/${target.page ?? ""}",
extra: subRoute == -1 ? 0 : subRoute,
);
} catch (e) {
throw Exception(e);
}
} else {
AppRouter.router.push(Routes.HOME);
}
}else {
AppRouter.router.push(Routes.HOME);
}
});
if (kDebugMode) {
print('_debugLabelString: $_debugLabelString');
}
OneSignal.InAppMessages.paused(true);
}
It is working when app is in background but not working when app is completely closed or cleared from background. And also added NotificationServiceExtension
and register it in Mainfeast
.