promize_sdk 0.2.1 promize_sdk: ^0.2.1 copied to clipboard
A data collection SDK provides an easy way to collect your users data and interests
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:promize_sdk/core/event_types.dart';
import 'package:promize_sdk/core/models/user.dart';
import 'package:promize_sdk/core/utils/notification_action.dart';
import 'package:promize_sdk/promize_sdk.dart';
import 'alert.dart';
import 'flavors.dart';
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final _promizeSdkPlugin = PromizeSdk.instance;
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
// await Firebase.initializeApp();
await _promizeSdkPlugin.initialize(
apiKey: '{your-api-key}',
siteId: '{your-site-id}',
baseUrl: '{your-base-url}',
pushEnabled: true,
appVersion: '0.0.1',
);
// track notification activity as received in background
_promizeSdkPlugin.trackNotificationActivity(
activityType: NotificationAction.received,
data: message.data,
);
}
Future<void> runMainApp(Flavor flavor) async {
WidgetsFlutterBinding.ensureInitialized();
// await Firebase.initializeApp(
// options: DefaultFirebaseOptions.currentPlatform,
// );
runApp(MaterialApp(
debugShowCheckedModeBanner: flavor == Flavor.development,
home: MyApp(
flavor: flavor,
),
));
}
class MyApp extends StatefulWidget {
final Flavor flavor;
const MyApp({required this.flavor});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _promizeSdkPlugin = PromizeSdk.instance;
String token = '';
EventType? _eventType = EventType.event;
TextEditingController _nameController = TextEditingController();
TextEditingController _emailController = TextEditingController();
TextEditingController _userIdController = TextEditingController();
TextEditingController _eventNameController = TextEditingController();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
setupFirebaseMessaging();
await _promizeSdkPlugin.initialize(
apiKey: '{your-api-key}',
siteId: '{your-site-id}',
baseUrl: widget.flavor == Flavor.production
? '{your-base-url}'
: '{your-base-url}',
pushEnabled: true,
appVersion: '0.0.1',
);
final user = _promizeSdkPlugin.getUser();
if (user != null) {
_nameController.text = user.name;
_emailController.text = user.email;
_userIdController.text = user.userId;
}
// addEvent();
//updateUser();
});
}
void createUser({String? name, String? email, String? userId}) {
_promizeSdkPlugin.createUser(
user: User(
name: name ?? "name",
email: email ?? "mail@gmail.com",
userId: userId ?? "44",
data: {
"type": "user",
"premium": true,
},
),
);
}
void updateUser() {
_promizeSdkPlugin.createUser(
user: const User(
name: "name",
email: "mail@gmail.com",
userId: "44",
),
);
}
void updateDeviceToken(String? token) {
if (token == null) return;
setState(() {
this.token = token;
});
debugPrint(token);
_promizeSdkPlugin.updateDeviceToken(
deviceToken: token,
);
}
void addEvent({String? eventName, EventType? eventType}) {
_promizeSdkPlugin.addEvent(
eventName: eventName ?? "test Event",
eventType: eventType ?? EventType.event,
eventData: {"first_collectd": "data"},
);
}
void setupFirebaseMessaging() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
final fcmToken = await messaging.getToken();
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
saveDeviceToken(fcmToken);
setState(() {
this.token = fcmToken ?? '';
});
debugPrint('User granted permission: ${settings.authorizationStatus}');
messaging.onTokenRefresh.listen((fcmToken) {
// Note: This callback is fired at each app startup and whenever a new
// token is generated.
updateDeviceToken(fcmToken);
}).onError((err) {
debugPrint(err.toString());
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
// track notification activity as received in foreground
_promizeSdkPlugin.trackNotificationActivity(
activityType: NotificationAction.received,
data: message.data,
);
if (message.notification != null) {
print(
'Message also contained a notification: ${message.notification?.title ?? ''} - ${message.notification?.body ?? ''}');
}
Alert.instance.showNotification(
context,
message.notification?.title,
message.notification?.body,
() {
// track notification activity as clicked
_promizeSdkPlugin.trackNotificationActivity(
activityType: NotificationAction.clicked,
data: message.data,
);
},
);
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
// track notification activity as clicked
_promizeSdkPlugin.trackNotificationActivity(
activityType: NotificationAction.clicked,
data: message.data,
);
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
Scaffold(
appBar: AppBar(
title: Text('Promize SDK ${widget.flavor.name}'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Text('FCM Token: '),
Flexible(
child: SelectableText(
token,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
)),
],
),
const SizedBox(height: 10),
Row(
children: [
Text('Email: '),
Flexible(
child: SelectableText(
_emailController.text,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
)),
],
),
const SizedBox(height: 10),
Row(
children: [
Text('User Id: '),
Flexible(
child: SelectableText(
_userIdController.text,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
)),
],
),
const SizedBox(height: 10),
TextField(
controller: _nameController,
decoration: const InputDecoration(
hintText: 'Name',
),
onChanged: (value) {},
),
const SizedBox(height: 10),
TextField(
controller: _emailController,
decoration: const InputDecoration(
hintText: 'Email',
),
onChanged: (value) {},
),
const SizedBox(height: 10),
TextField(
controller: _userIdController,
decoration: const InputDecoration(
hintText: 'UserID',
),
onChanged: (value) {},
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
createUser(
name: _nameController.text,
email: _emailController.text,
userId: _userIdController.text,
);
setState(() {});
},
child: const Text('Create User'),
),
if (_promizeSdkPlugin.getUser() != null) ...[
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
_promizeSdkPlugin.logout();
setState(() {});
},
child: const Text('Logout'),
),
],
const SizedBox(height: 30),
TextField(
controller: _eventNameController,
decoration: const InputDecoration(
hintText: 'Event Name',
),
onChanged: (value) {},
),
const SizedBox(height: 16),
DropdownButton<EventType>(
value: _eventType,
isExpanded: true,
items: EventType.values
.map((e) => DropdownMenuItem<EventType>(
value: e, child: Text(e.name)))
.toList(),
onChanged: (value) {
setState(() {
_eventType = value!;
});
}),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
addEvent(
eventName: _eventNameController.text,
eventType: _eventType,
);
setState(() {});
},
child: const Text('Add Event'),
),
const SizedBox(height: 30),
],
),
),
),
),
),
Align(
alignment: Alignment.topRight,
child: SafeArea(
child: Container(
padding: const EdgeInsets.only(top: 55, right: 50),
child: const Banner(
message: "demo",
location: BannerLocation.bottomStart,
),
),
),
),
],
);
}
void saveDeviceToken(String? fcmToken) {
if (fcmToken == null) return;
_promizeSdkPlugin.saveDeviceToken(deviceToken: fcmToken);
}
}