// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/actions/actions.dart' as action_blocks;
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
Future handleNotificationClick() async {
OneSignal.Notifications.addClickListener(
(OSNotificationClickEvent result) async {
final additionalData = result.notification.additionalData;
if (additionalData == null || additionalData['type'] == null) {
debugPrint('Notification Click Handling Error: Missing additional data');
return;
}
final navigateTo = additionalData['type'];
try {
switch (navigateTo) {
case "EVENT":
Article article=
await getArticleDocument(additionalData['eventId']);
appNavigatorKey.currentState!.context.pushNamed(
'ArticleDetails',
queryParameters: {
'article': serializeParam(
article,
ParamType.Document,
),
}.withoutNulls,
extra: <String, dynamic>{
'event': article,
},
);
break;
default:
debugPrint('Unhandled notification type: $navigateTo');
}
} catch (e) {
debugPrint('Notification Click Handling Error: $e');
}
});
}
// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the green button on the right!
๐ OneSignal Notification Click Handler
๐ Overview
This custom action listens for click events on OneSignal push notifications and seamlessly navigates users to the relevant screen based on the notification's additional data. It is designed primarily for event-related notifications, fetching event details and triggering the appropriate navigation action.
โ๏ธ Implementation Details:
โ
To implement, simply call this action in the main.dart
file as an initial action and modify the logic to align with your specific page structure and database requirements.
๐ฏ Conclusion
This integration ensures a seamless navigation to the specific page in the app and it'll work on all the state of the Mobile App(Foreground, background, Terminated).
If you found this documentation helpful, please โ
like, ๐ share, and ๐ฌ comment!
#Flutterflow #OneSignal #Push Notification