feeddo_push_notification_apns 0.1.1
feeddo_push_notification_apns: ^0.1.1 copied to clipboard
Direct Apple Push Notification service (APNs) bridge for Flutter on iOS. Lightweight APNs token management, foreground presentation, and tap handling without Firebase.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:feeddo_push_notification_apns/feeddo_push_notification_apns.dart';
void main() {
runApp(const MaterialApp(
debugShowCheckedModeBanner: false,
home: FeeddoApnsExampleApp(),
));
}
class FeeddoApnsExampleApp extends StatefulWidget {
const FeeddoApnsExampleApp({super.key});
@override
State<FeeddoApnsExampleApp> createState() => _FeeddoApnsExampleAppState();
}
class _FeeddoApnsExampleAppState extends State<FeeddoApnsExampleApp> {
final _apns = FeeddoPushNotificationApns.instance;
String? _token;
bool _permissionGranted = false;
ApnsNotificationSettings? _settings;
final List<String> _eventLog = [];
@override
void initState() {
super.initState();
_setupApns();
}
Future<void> _setupApns() async {
// 1. Listen to live token updates
_apns.onToken.listen((token) {
setState(() {
_token = token;
_eventLog.insert(0, '[onToken] Received APNs token: $token');
});
});
// 2. Listen to notification opened / tapped
_apns.onNotificationOpened.listen((payload) {
setState(() {
_eventLog.insert(0, '[onNotificationOpened] Tapped payload: $payload');
});
});
// 3. Listen to foreground notification arrival
_apns.onForegroundNotification.listen((payload) {
setState(() {
_eventLog.insert(
0, '[onForegroundNotification] Foreground payload: $payload');
});
});
// 4. Initialize native bridge
final initResult = await _apns.initialize(autoRegister: true);
final settings = await _apns.getNotificationSettings();
setState(() {
_token = initResult.token;
_settings = settings;
_permissionGranted = settings.isAuthorized;
if (initResult.token != null) {
_eventLog.insert(0, '[init] Cached APNs token: ${initResult.token}');
}
if (initResult.initialNotification != null) {
_eventLog.insert(0,
'[init] Launched from notification: ${initResult.initialNotification}');
}
});
}
Future<void> _requestPermissions() async {
final granted = await _apns.requestNotificationPermissions(
alert: true,
badge: true,
sound: true,
);
final settings = await _apns.getNotificationSettings();
setState(() {
_permissionGranted = granted;
_settings = settings;
_eventLog.insert(
0, '[permission] Permission granted: $granted ($settings)');
});
}
Future<void> _refreshRegistration() async {
final token = await _apns.registerForRemoteNotifications();
setState(() {
_token = token;
_eventLog.insert(0, '[register] Re-registered token: $token');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF8FAFC),
appBar: AppBar(
title: const Text('Feeddo APNs Push Example'),
backgroundColor: Colors.white,
foregroundColor: const Color(0xFF0F172A),
elevation: 0.5,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Token Card
Card(
elevation: 0,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: Color(0xFFE2E8F0)),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'APNs Device Token',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Color(0xFF475569),
),
),
const SizedBox(height: 8),
SelectableText(
_token ?? 'Token not yet received (registering...)',
style: TextStyle(
fontSize: 12,
fontFamily: 'Courier',
color: _token != null
? const Color(0xFF0F172A)
: Colors.grey,
),
),
const SizedBox(height: 12),
Row(
children: [
ElevatedButton.icon(
onPressed: _token != null
? () {
Clipboard.setData(
ClipboardData(text: _token!));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Token copied!')),
);
}
: null,
icon: const Icon(Icons.copy, size: 16),
label: const Text('Copy Token'),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
foregroundColor: Colors.white,
),
),
const SizedBox(width: 8),
OutlinedButton.icon(
onPressed: _refreshRegistration,
icon: const Icon(Icons.refresh, size: 16),
label: const Text('Re-register'),
),
],
),
],
),
),
),
const SizedBox(height: 12),
// Permissions Card
Card(
elevation: 0,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: Color(0xFFE2E8F0)),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'iOS Push Permissions',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Color(0xFF0F172A),
),
),
const SizedBox(height: 4),
Text(
_settings?.authorizationStatus.name ??
'Not Determined',
style: TextStyle(
fontSize: 12,
color: _permissionGranted
? Colors.green
: Colors.orange,
fontWeight: FontWeight.w500,
),
),
],
),
ElevatedButton(
onPressed: _requestPermissions,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF0F172A),
foregroundColor: Colors.white,
),
child: const Text('Request Permission'),
),
],
),
),
),
const SizedBox(height: 16),
const Text(
'Live Notification Event Log',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w600,
color: Color(0xFF334155),
),
),
const SizedBox(height: 8),
Expanded(
child: Container(
decoration: BoxDecoration(
color: const Color(0xFF0F172A),
borderRadius: BorderRadius.circular(12),
),
padding: const EdgeInsets.all(12),
child: _eventLog.isEmpty
? const Center(
child: Text(
'No events logged yet.\nSend an APNs push to see it here live.',
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF94A3B8),
fontSize: 12,
),
),
)
: ListView.separated(
itemCount: _eventLog.length,
separatorBuilder: (context, index) => const Divider(
color: Color(0xFF1E293B),
height: 12,
),
itemBuilder: (context, index) {
return Text(
_eventLog[index],
style: const TextStyle(
color: Color(0xFFE2E8F0),
fontSize: 11,
fontFamily: 'Courier',
),
);
},
),
),
),
],
),
),
),
);
}
}