bnotify_flutter 0.1.0
bnotify_flutter: ^0.1.0 copied to clipboard
BNotify Flutter plugin wrapping the native Android and iOS SDKs for push notifications and in-app messaging.
example/lib/main.dart
import 'dart:async';
import 'package:bnotify_flutter/bnotify_flutter.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'Starting…';
String _token = '—';
String _lastMessage = '—';
String _lastClick = '—';
final List<StreamSubscription<dynamic>> _subs = [];
@override
void initState() {
super.initState();
_start();
}
Future<void> _start() async {
_subs.add(
BNotify.onToken.listen((token) {
if (!mounted) return;
setState(() => _token = token);
}),
);
_subs.add(
BNotify.onMessage.listen((message) {
if (!mounted) return;
setState(() {
_lastMessage = '${message.title ?? ''} ${message.body ?? ''}'.trim();
});
}),
);
_subs.add(
BNotify.onClick.listen((click) {
if (!mounted) return;
setState(() {
_lastClick = click.screen ?? click.notificationId ?? click.action ?? 'tap';
});
}),
);
try {
await BNotify.initialize(configAsset: 'assets/bnotify-config.json');
if (!mounted) return;
setState(() {
_status =
'Initialized. Replace placeholder Console credentials, then request permission.';
});
} catch (error) {
if (!mounted) return;
setState(() {
_status =
'Init failed (expected until Console config is added):\n$error';
});
}
}
@override
void dispose() {
for (final sub in _subs) {
sub.cancel();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('BNotify Flutter')),
body: Padding(
padding: const EdgeInsets.all(16),
child: ListView(
children: [
Text(_status),
const SizedBox(height: 16),
Text('Token: $_token'),
const SizedBox(height: 8),
Text('Last message: $_lastMessage'),
const SizedBox(height: 8),
Text('Last click: $_lastClick'),
const SizedBox(height: 24),
FilledButton(
onPressed: () => BNotify.requestPermission(),
child: const Text('Request permission'),
),
const SizedBox(height: 8),
OutlinedButton(
onPressed: () => BNotify.logEvent('app_open'),
child: const Text('logEvent("app_open")'),
),
],
),
),
),
);
}
}