cc_launch_notification_plugin 0.0.2
cc_launch_notification_plugin: ^0.0.2 copied to clipboard
A Flutter plugin to capture push notifications when app launches from killed state on iOS.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:cc_launch_notification_plugin/cc_launch_notification_plugin.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
LaunchNotification? _launchNotification;
String _status = '等待检查...';
bool _isLoading = false;
@override
void initState() {
super.initState();
Future.delayed(const Duration(milliseconds: 500), () {
_checkLaunchNotification();
});
}
Future<void> _checkLaunchNotification() async {
setState(() {
_isLoading = true;
_status = '正在检查...';
});
try {
final notification = await CcLaunchNotificationPlugin.consumeLaunchNotification();
setState(() {
_launchNotification = notification;
_status = notification != null ? '✅ 发现启动通知' : '❌ 无启动通知';
_isLoading = false;
});
if (notification != null) {
_showNotificationDialog(notification);
}
} catch (e) {
setState(() {
_status = '❌ 错误: $e';
_isLoading = false;
});
}
}
void _showNotificationDialog(LaunchNotification notification) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('🔔 启动通知详情'),
content: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
_buildInfoRow('标题', notification.title ?? '-'),
_buildInfoRow('内容', notification.body ?? '-'),
_buildInfoRow('接收时间', notification.receivedTime?.toString() ?? '-'),
const Divider(height: 20),
const Text('原始数据:', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
Text(
notification.data.toString(),
style: const TextStyle(fontSize: 12),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('关闭'),
),
],
),
);
}
Widget _buildInfoRow(String label, String value) {
return Padding(
padding: const EdgeInsets.only(bottom: 8),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 80,
child: Text('$label:', style: const TextStyle(fontWeight: FontWeight.bold)),
),
Expanded(child: Text(value)),
],
),
);
}
Future<void> _getLaunchNotification() async {
setState(() => _isLoading = true);
final notification = await CcLaunchNotificationPlugin.getLaunchNotification();
setState(() {
_status = notification != null ? '✅ 获取成功(未消费)' : '❌ 无数据或已消费';
_isLoading = false;
});
if (notification != null) {
_showNotificationDialog(notification);
}
}
Future<void> _clearNotification() async {
setState(() => _isLoading = true);
final success = await CcLaunchNotificationPlugin.clearLaunchNotification();
setState(() {
_launchNotification = null;
_status = success ? '✅ 已清除' : '❌ 清除失败';
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Launch Notification Plugin'),
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Text(
_status,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
if (_isLoading) ...[
const SizedBox(height: 16),
const CircularProgressIndicator(),
],
],
),
),
),
const SizedBox(height: 20),
if (_launchNotification != null) ...[
Card(
color: Colors.blue[50],
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('通知摘要:', style: TextStyle(fontWeight: FontWeight.bold)),
const SizedBox(height: 8),
Text('标题: ${_launchNotification!.title ?? "无"}'),
Text('内容: ${_launchNotification!.body ?? "无"}'),
Text('时间: ${_launchNotification!.receivedTime}'),
],
),
),
),
const SizedBox(height: 20),
],
ElevatedButton.icon(
onPressed: _isLoading ? null : _checkLaunchNotification,
icon: const Icon(Icons.refresh),
label: const Text('重新检查(消费)'),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _isLoading ? null : _getLaunchNotification,
icon: const Icon(Icons.visibility),
label: const Text('获取通知(不消费)'),
),
const SizedBox(height: 12),
OutlinedButton.icon(
onPressed: _isLoading ? null : _clearNotification,
icon: const Icon(Icons.clear),
label: const Text('清除通知'),
style: OutlinedButton.styleFrom(foregroundColor: Colors.red),
),
const Spacer(),
const Card(
color: Colors.amber,
child: Padding(
padding: EdgeInsets.all(12),
child: Text(
'💡 测试方法:\n'
'1. 完全关闭APP\n'
'2. 发送远程推送通知\n'
'3. 点击通知启动APP\n'
'4. 插件会自动捕获通知数据',
style: TextStyle(fontSize: 12),
),
),
),
],
),
),
),
);
}
}