flutter_pushed_messaging 1.6.8
flutter_pushed_messaging: ^1.6.8 copied to clipboard
A Flutter plugin for use Multipushed messaging system.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_pushed_messaging/flutter_pushed_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:url_launcher/url_launcher.dart';
@pragma('vm:entry-point')
Future<void> backgroundMessage(Map<dynamic, dynamic> message) async {
WidgetsFlutterBinding.ensureInitialized();
print("Backgroung message: $message");
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterPushedMessaging.init(
backgroundMessage,
notificationChannel: "messages",
askPermissions: true,
loggerEnabled: true,
);
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ServiceStatus _status = FlutterPushedMessaging.status;
String _title = '';
String _body = '';
String _token = '';
final FlutterLocalNotificationsPlugin _localNotifications =
FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
await _initLocalNotifications();
// Handle message which opened the app from a terminated state
final initialMessage = await FlutterPushedMessaging.getInitialMessage();
if (initialMessage != null) {
print("onMessageOpenedApp (from terminated): $initialMessage");
_maybeOpenDeepLinkFromMessage(initialMessage);
}
// Handle message which opened the app from a background state
FlutterPushedMessaging.onMessageOpenedApp().listen((message) async {
print("onMessageOpenedApp (from background): $message");
_maybeOpenDeepLinkFromMessage(message);
});
FlutterPushedMessaging.onMessage().listen((message) {
print("Message: $message");
String title = "";
String body = "";
dynamic dataField = message["data"];
// If the data field is JSON-string or Map – handle both
if (dataField is String) {
if (dataField.isNotEmpty) {
try {
final parsed = json.decode(dataField);
if (parsed is Map) {
title = parsed["title"]?.toString() ?? "";
body = parsed["body"]?.toString() ?? "";
}
} catch (_) {
// Not JSON – ignore
}
}
} else if (dataField is Map) {
title = dataField["title"]?.toString() ?? "";
body = dataField["body"]?.toString() ?? "";
}
if (title.isEmpty && body.isEmpty) {
// Fallback to pushedNotification field if present
final notif = message["pushedNotification"];
if (notif is Map) {
title = notif["Title"]?.toString() ?? "";
body = notif["Body"]?.toString() ?? "";
}
}
if (title.isNotEmpty || body.isNotEmpty) {
setState(() {
_title = title;
_body = body;
});
_showForegroundNotification(title, body, message);
}
});
FlutterPushedMessaging.onStatus().listen((status) {
setState(() {
_status = status;
});
});
if (!mounted) return;
setState(() {
_title = '';
_token = FlutterPushedMessaging.token ?? "";
_status = FlutterPushedMessaging.status;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Pushed messaging plugin example app'),
),
body: Center(
child: Column(
children: [
Text("Service status: $_status",
style: Theme.of(context).textTheme.titleMedium),
Text(_title, style: Theme.of(context).textTheme.titleMedium),
Text(
_body,
style: Theme.of(context).textTheme.titleMedium,
textAlign: TextAlign.center,
maxLines: 3,
),
const SizedBox(height: 12),
Text(
_token,
style: Theme.of(context).textTheme.titleMedium,
),
TextButton(
onPressed: () async {
await Clipboard.setData(ClipboardData(text: _token));
},
child: Text("Copy token",
style: Theme.of(context).textTheme.titleMedium)),
TextButton(
onPressed: () async {
await Clipboard.setData(ClipboardData(
text: await FlutterPushedMessaging.getLog() ?? ""));
},
child: Text("Get Log",
style: Theme.of(context).textTheme.titleMedium)),
const SizedBox(height: 8),
TextButton(
onPressed: _showTestLocalDeepLinkNotification,
child: Text("Send local deep link notification",
style: Theme.of(context).textTheme.titleMedium)),
],
),
),
),
);
}
Future<void> _initLocalNotifications() async {
final InitializationSettings initSettings = InitializationSettings(
iOS: const DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
),
android: const AndroidInitializationSettings('@mipmap/ic_launcher'),
);
await _localNotifications.initialize(
initSettings,
onDidReceiveNotificationResponse: (resp) async {
final payload = resp.payload;
if (payload != null && payload.isNotEmpty) {
_handleDeepLinkPayload(payload);
}
},
);
final details = await _localNotifications.getNotificationAppLaunchDetails();
if ((details?.didNotificationLaunchApp ?? false)) {
final payload = details!.notificationResponse?.payload;
if (payload != null && payload.isNotEmpty) {
_handleDeepLinkPayload(payload);
}
}
}
Future<void> _showTestLocalDeepLinkNotification() async {
final String payload = json.encode({
"url":
"https://example.com/deeplink-test?ts=${DateTime.now().millisecondsSinceEpoch}",
});
const NotificationDetails details = NotificationDetails(
iOS: DarwinNotificationDetails(subtitle: "Tap to open deep link"),
android: AndroidNotificationDetails(
'test_deeplink',
'Test Deeplink',
channelDescription: 'Channel for deeplink test',
importance: Importance.high,
priority: Priority.high,
icon: '@mipmap/ic_launcher',
),
);
await _localNotifications.show(
1,
'Deep link test',
'Tap to open the URL',
details,
payload: payload,
);
}
Future<void> _showForegroundNotification(
String title, String body, Map<dynamic, dynamic> message) async {
final String payload = json.encode({
"url": _getDeepLinkUrlFromMessage(message) ?? "",
});
const NotificationDetails details = NotificationDetails(
iOS: DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
),
android: AndroidNotificationDetails(
'messages',
'Messages',
channelDescription: 'Pushed messages',
importance: Importance.high,
priority: Priority.high,
icon: '@mipmap/ic_launcher',
),
);
await _localNotifications.show(
DateTime.now().millisecondsSinceEpoch.toUnsigned(31),
title,
body,
details,
payload: payload,
);
}
void _maybeOpenDeepLinkFromMessage(Map<dynamic, dynamic> message) async {
final url = _getDeepLinkUrlFromMessage(message);
if (url == null || url.isEmpty) return;
final uri = Uri.tryParse(url);
if (uri == null) return;
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
}
String? _getDeepLinkUrlFromMessage(Map<dynamic, dynamic> message) {
String? url;
// Common locations: data.url, pushedNotification.url, top-level url
final data = message['data'];
if (data is Map && data['url'] is String) {
url = data['url'] as String;
} else if (message['pushedNotification'] is Map &&
(message['pushedNotification']['url'] is String)) {
url = message['pushedNotification']['url'] as String;
} else if (message['url'] is String) {
url = message['url'] as String;
}
return url;
}
void _handleDeepLinkPayload(String payload) {
try {
final decoded = json.decode(payload);
if (decoded is Map && decoded['url'] is String) {
final url = decoded['url'] as String;
final uri = Uri.tryParse(url);
if (uri != null) {
launchUrl(uri, mode: LaunchMode.externalApplication);
return;
}
}
} catch (_) {
// Payload might not be JSON; treat as raw URL
final uri = Uri.tryParse(payload);
if (uri != null) {
launchUrl(uri, mode: LaunchMode.externalApplication);
}
}
}
}