|
1 | 1 | import 'package:flutter/material.dart'; |
2 | 2 | import 'package:pnta_flutter/pnta_flutter.dart'; |
3 | 3 |
|
4 | | -void main() { |
5 | | - runApp(const MyApp()); |
| 4 | +void main() async { |
| 5 | + WidgetsFlutterBinding.ensureInitialized(); |
| 6 | + |
| 7 | + await PntaFlutter.initialize( |
| 8 | + 'prj_k3e0Givq', // replace with your project id |
| 9 | + metadata: { |
| 10 | + 'user_id': '123', |
| 11 | + 'user_email': 'user@example.com', |
| 12 | + }, |
| 13 | + autoHandleLinks: true, |
| 14 | + showSystemUI: true, |
| 15 | + ); |
| 16 | + |
| 17 | + runApp(MyApp()); |
6 | 18 | } |
7 | 19 |
|
8 | 20 | class MyApp extends StatelessWidget { |
9 | 21 | const MyApp({super.key}); |
| 22 | + |
10 | 23 | @override |
11 | 24 | Widget build(BuildContext context) { |
12 | 25 | return MaterialApp( |
13 | 26 | navigatorKey: PntaFlutter.navigatorKey, |
14 | | - home: const HomeScreen(), |
| 27 | + home: HomePage(), |
| 28 | + routes: { |
| 29 | + '/profile': (context) => ProfilePage(), |
| 30 | + }, |
15 | 31 | ); |
16 | 32 | } |
17 | 33 | } |
18 | 34 |
|
19 | | -class HomeScreen extends StatefulWidget { |
20 | | - const HomeScreen({super.key}); |
| 35 | +class HomePage extends StatefulWidget { |
| 36 | + const HomePage({super.key}); |
| 37 | + |
21 | 38 | @override |
22 | | - State<HomeScreen> createState() => _HomeScreenState(); |
| 39 | + State<HomePage> createState() => _HomePageState(); |
23 | 40 | } |
24 | 41 |
|
25 | | -class _HomeScreenState extends State<HomeScreen> { |
26 | | - String? _deviceToken; |
27 | | - String? _lastNotificationTap; |
28 | | - String? _foregroundLink; |
29 | | - final List<Map<String, dynamic>> _foregroundNotifications = []; |
30 | | - bool _loading = true; |
| 42 | +class _HomePageState extends State<HomePage> { |
| 43 | + String? _lastNotification; |
31 | 44 |
|
32 | 45 | @override |
33 | 46 | void initState() { |
34 | 47 | super.initState(); |
35 | | - _initPnta(); |
| 48 | + |
36 | 49 | PntaFlutter.foregroundNotifications.listen((payload) { |
37 | | - setState(() { |
38 | | - _foregroundNotifications.insert(0, payload); |
39 | | - final link = payload['link_to'] as String?; |
40 | | - if (link != null && link.isNotEmpty) { |
41 | | - _foregroundLink = link; |
42 | | - } |
43 | | - }); |
| 50 | + setState(() => _lastNotification = 'Foreground: ${payload.toString()}'); |
44 | 51 | }); |
| 52 | + |
45 | 53 | PntaFlutter.onNotificationTap.listen((payload) { |
46 | | - setState(() { |
47 | | - _lastNotificationTap = payload.toString(); |
48 | | - }); |
| 54 | + setState(() => _lastNotification = 'Tap: ${payload.toString()}'); |
49 | 55 | }); |
50 | 56 | } |
51 | 57 |
|
52 | | - Future<void> _initPnta() async { |
53 | | - await PntaFlutter.initialize( |
54 | | - 'prj_k3e0Givq', |
55 | | - metadata: { |
56 | | - 'user_id': '123', |
57 | | - 'email': 'user@example.com', |
58 | | - 'role': 'tester', |
59 | | - }, |
60 | | - autoHandleLinks: false, // We'll handle links manually for demo |
| 58 | + @override |
| 59 | + Widget build(BuildContext context) { |
| 60 | + return Scaffold( |
| 61 | + appBar: AppBar(title: Text('PNTA Example')), |
| 62 | + body: Center( |
| 63 | + child: Column( |
| 64 | + mainAxisAlignment: MainAxisAlignment.center, |
| 65 | + children: [ |
| 66 | + Icon(Icons.notifications, size: 64, color: Colors.blue), |
| 67 | + SizedBox(height: 16), |
| 68 | + Text( |
| 69 | + 'Push notifications ready!', |
| 70 | + style: Theme.of(context).textTheme.headlineSmall, |
| 71 | + ), |
| 72 | + SizedBox(height: 8), |
| 73 | + Text( |
| 74 | + 'Device token: ${PntaFlutter.deviceToken != null ? "Available" : "Not available"}'), |
| 75 | + if (PntaFlutter.deviceToken != null) |
| 76 | + SelectableText( |
| 77 | + PntaFlutter.deviceToken!, |
| 78 | + style: TextStyle(fontSize: 10, fontFamily: 'monospace'), |
| 79 | + ), |
| 80 | + if (_lastNotification != null) ...[ |
| 81 | + SizedBox(height: 16), |
| 82 | + Padding( |
| 83 | + padding: EdgeInsets.symmetric(horizontal: 24), |
| 84 | + child: SelectableText( |
| 85 | + _lastNotification!, |
| 86 | + style: TextStyle(fontSize: 10, fontFamily: 'monospace'), |
| 87 | + ), |
| 88 | + ), |
| 89 | + ], |
| 90 | + ], |
| 91 | + ), |
| 92 | + ), |
61 | 93 | ); |
62 | | - setState(() { |
63 | | - _deviceToken = PntaFlutter.deviceToken; |
64 | | - _loading = false; |
65 | | - }); |
66 | 94 | } |
| 95 | +} |
| 96 | + |
| 97 | +class ProfilePage extends StatelessWidget { |
| 98 | + const ProfilePage({super.key}); |
67 | 99 |
|
68 | 100 | @override |
69 | 101 | Widget build(BuildContext context) { |
70 | 102 | return Scaffold( |
71 | | - appBar: AppBar(title: const Text('PNTA Metadata & Foreground Demo')), |
72 | | - body: Padding( |
73 | | - padding: const EdgeInsets.all(24), |
74 | | - child: _loading |
75 | | - ? const Center(child: CircularProgressIndicator()) |
76 | | - : Column( |
77 | | - crossAxisAlignment: CrossAxisAlignment.start, |
78 | | - children: [ |
79 | | - const Text('Device Token:'), |
80 | | - SelectableText(_deviceToken ?? 'No token (denied or error)'), |
81 | | - const SizedBox(height: 24), |
82 | | - const Text('Foreground Notifications:'), |
83 | | - if (_foregroundNotifications.isEmpty) const Text('None'), |
84 | | - for (final notif in _foregroundNotifications) |
85 | | - Card( |
86 | | - child: Padding( |
87 | | - padding: const EdgeInsets.all(8.0), |
88 | | - child: Text(notif.toString()), |
89 | | - ), |
90 | | - ), |
91 | | - const SizedBox(height: 24), |
92 | | - if (_foregroundLink != null) ...[ |
93 | | - ElevatedButton( |
94 | | - onPressed: () async { |
95 | | - final link = _foregroundLink; |
96 | | - if (link != null) { |
97 | | - await PntaFlutter.handleLink(link); |
98 | | - setState(() { |
99 | | - _foregroundLink = null; |
100 | | - }); |
101 | | - } |
102 | | - }, |
103 | | - child: const Text('Open Foreground Link'), |
104 | | - ), |
105 | | - const SizedBox(height: 16), |
106 | | - ], |
107 | | - const Text('Last Notification Tap Payload:'), |
108 | | - SelectableText(_lastNotificationTap ?? 'None'), |
109 | | - ], |
110 | | - ), |
| 103 | + appBar: AppBar(title: Text('Profile')), |
| 104 | + body: Center( |
| 105 | + child: Text('Profile page opened via deep link!'), |
111 | 106 | ), |
112 | 107 | ); |
113 | 108 | } |
|
0 commit comments