katyayani_core 1.0.0
katyayani_core: ^1.0.0 copied to clipboard
Katyayani Core SDK — Push notifications (sticky, timer, standard), nudges, event tracking for Flutter apps.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:katyayani_core/katyayani_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// ─── Initialize SDK ───
await KatyayaniCore.init(KCConfig(
siteId: 'nc_live_your_api_key_here',
apiBase: 'http://localhost:3001',
debug: true,
));
// ─── Set notification callbacks ───
KatyayaniCore.onNotificationAction((notification, action) {
print('Notification ${notification.id}: ${action.name}');
// Handle deep links
if (notification.actionUrl != null) {
// Navigate to URL/route
}
// Handle timer expiry
if (action == KCNotificationAction.timerExpired) {
print('Timer expired for: ${notification.id}');
}
});
KatyayaniCore.onNotificationButtonAction((notification, buttonId) {
print('Button tapped: $buttonId on ${notification.id}');
});
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Katyayani Organics',
navigatorObservers: [KCNavigationObserver()],
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
// Identify user after login
KatyayaniCore.identify('user_123', traits: {
'email': 'farmer@example.com',
'name': 'Rajesh Kumar',
'plan': 'premium',
});
// Request push permission
KatyayaniCore.requestPushPermission();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Katyayani Organics')),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
// ─── Track Events ───
ElevatedButton(
onPressed: () {
KatyayaniCore.track('product_viewed', properties: {
'product': 'Humic Acid',
'price': 499,
'category': 'Fertilizer',
});
},
child: const Text('View Product (Humic Acid)'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
KatyayaniCore.track('add_to_cart', properties: {
'product': 'Humic Acid',
'quantity': 2,
});
},
child: const Text('Add to Cart'),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
KatyayaniCore.track('checkout_completed', properties: {
'order_id': 'ORD-123',
'total': 998,
});
},
child: const Text('Complete Checkout'),
),
const Divider(height: 32),
// ─── Test Notifications ───
const Text('Test Notifications', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
// Standard notification
ElevatedButton(
onPressed: () {
KatyayaniCore.showNotification(KCNotification(
id: 'test_standard_1',
title: 'New Offer!',
body: 'Humic Acid is now available at ₹399. Limited time!',
type: KCNotificationType.standard,
actionUrl: '/products/humic-acid',
));
},
child: const Text('Standard Notification'),
),
const SizedBox(height: 8),
// Sticky notification
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.orange),
onPressed: () {
KatyayaniCore.showNotification(KCNotification(
id: 'test_sticky_1',
title: '🛒 Order Processing',
body: 'Your order ORD-123 is being packed. Track your order.',
type: KCNotificationType.sticky,
sticky: true,
buttons: [
KCNotificationButton(id: 'track', label: 'Track Order', deepLink: '/orders/ORD-123'),
KCNotificationButton(id: 'details', label: 'View Details'),
],
));
},
child: const Text('Sticky Notification (with buttons)'),
),
const SizedBox(height: 8),
// Timer notification
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
onPressed: () {
KatyayaniCore.showNotification(KCNotification(
id: 'test_timer_1',
title: '⏰ Flash Sale Ending!',
body: 'Flat 50% off on all fertilizers',
type: KCNotificationType.timer,
timerDurationSeconds: 120, // 2 minutes
timerText: 'Offer expires in {timer}',
timerAction: KCTimerAction.showFollowUp,
timerActionPayload: 'Sale ended! But here\'s 20% off just for you.',
buttons: [
KCNotificationButton(id: 'shop', label: 'Shop Now', deepLink: '/sale'),
],
));
},
child: const Text('Timer Notification (2 min countdown)'),
),
const SizedBox(height: 8),
// Dismiss sticky
OutlinedButton(
onPressed: () => KatyayaniCore.dismissStickyNotification('test_sticky_1'),
child: const Text('Dismiss Sticky'),
),
// Clear all
OutlinedButton(
onPressed: () => KatyayaniCore.clearAllNotifications(),
child: const Text('Clear All Notifications'),
),
const Divider(height: 32),
// ─── Test Nudges ───
const Text('Test Nudges', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
ElevatedButton(
onPressed: () {
KatyayaniCore.checkNudges(context, '/home');
},
child: const Text('Check Nudges for /home'),
),
const Divider(height: 32),
// ─── Flush & Reset ───
OutlinedButton(
onPressed: () => KatyayaniCore.flushEvents(),
child: const Text('Flush Events Now'),
),
OutlinedButton(
onPressed: () => KatyayaniCore.resetIdentity(),
child: const Text('Logout (Reset Identity)'),
),
],
),
);
}
}