plotline_engage 5.0.6
plotline_engage: ^5.0.6 copied to clipboard
Flutter plugin for seamless integration of Plotline, a platform empowering growth and marketing teams to improve feature adoption and activation with in-app messages
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:plotline_engage/plotline.dart';
import 'package:plugin_plotline_example/routing/delegate.dart';
import 'package:plugin_plotline_example/routing/parser.dart';
import 'package:plugin_plotline_example/routing/route_state.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'firebase_options.dart';
import 'screens/home.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await initialiseFirebase();
if(Platform.isAndroid) {
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _navigatorKey = GlobalKey<NavigatorState>();
late final RouteState _routeState;
late final SimpleRouterDelegate _routerDelegate;
late final TemplateRouteParser _routeParser;
@override
void initState() {
_routeParser = TemplateRouteParser(
allowedPaths: [
'/home',
'/artists',
'/songs',
'/song/:songId',
'/artist/:artistId',
],
initialRoute: '/home',
);
_routeState = RouteState(_routeParser);
_routerDelegate = SimpleRouterDelegate(
routeState: _routeState,
navigatorKey: _navigatorKey,
builder: (context) => HomePage(
navigatorKey: _navigatorKey,
),
);
Plotline.setPlotlineNotificationClickListener((properties) => {
print("Plotline Notification Click Callback with properties: $properties")
});
Plotline.debug(true);
Plotline.setShouldEnableFlutterWidgetTouch(true);
Plotline.init("MDMxMjc4ZTctYTVhMi00OWY1LTgxMWItNWZlNzY2Mzg3MTll", "<userId>");
if(Platform.isIOS) {Plotline.requestPushPermission();}
Plotline.isFeatureEnabled("<featureFlag>").then((value) => print("[Feature Flag] ${value}"));
Plotline.getFeatureFlagPayload("<featureFlag>").then((value) => print("[Feature Flag] ${value}"));
Plotline.getFeatureFlag("<featureFlag>").then((value) => print("[Feature Flag] ${value}"));
Plotline.setPlotlineEventsListener((eventName, properties) => {
// Your callback implementation here
print("Plotline Callback for event: $eventName with properties: $properties")
});
Plotline.setPlotlineRedirectListener((properties) => {
// Your callback implementation here
print("Plotline Redirect Callback with properties: $properties")
});
super.initState();
}
@override
void dispose() {
_routeState.dispose();
_routerDelegate.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return PlotlineWrapper(
child: RouteStateScope(
notifier: _routeState,
child: MaterialApp.router(
routerDelegate: _routerDelegate,
routeInformationParser: _routeParser,
),
)
);
}
}
Future<void> initialiseFirebase() async {
FirebaseApp res = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
debugPrint("firebase project id: ${res.options.projectId}");
final messaging = FirebaseMessaging.instance;
if(Platform.isAndroid) {
NotificationSettings notificationSettings = await messaging.requestPermission(provisional: true);
String? fcmToken;
if(notificationSettings.authorizationStatus==AuthorizationStatus.authorized){
debugPrint("Granted permission!");
try {
fcmToken = await messaging.getToken();
} on Exception catch (e) {
debugPrint(e.toString());
}
}
debugPrint("fcm token: $fcmToken");
if(fcmToken!=null){
Plotline.setFcmToken(fcmToken);
// Set up foreground message handler
FirebaseMessaging.onMessage.listen((RemoteMessage message){
debugPrint("Message received!");
Plotline.showNotification(message.data);
});
}
messaging.onTokenRefresh.listen((receivedToken) {
fcmToken = receivedToken;
if(fcmToken!=null) {
Plotline.setFcmToken(fcmToken!);
}
});
}
}
// Set up background message handler
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
debugPrint("Background Message received!");
Plotline.showNotification(message.data);
}