Skip to content

Commit d5d529c

Browse files
committed
feat(linux): migrate to xdg-desktop-portal for notifications
The xdg-desktop-portal is the new standard for desktop notifications and is supported by GNOME, KDE, and other desktop environments. It also works with Flatpak and Snap applications. Resolves #1757
1 parent 067a6cb commit d5d529c

16 files changed

+491
-2318
lines changed

flutter_local_notifications/example/lib/main.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:convert';
33
import 'dart:io';
44
// ignore: unnecessary_import
55
import 'dart:typed_data';
6+
import 'dart:ui';
67

78
import 'package:device_info_plus/device_info_plus.dart';
89
import 'package:flutter/cupertino.dart';
@@ -12,7 +13,6 @@ import 'package:flutter/services.dart';
1213
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
1314
import 'package:flutter_timezone/flutter_timezone.dart';
1415
import 'package:http/http.dart' as http;
15-
import 'package:image/image.dart' as image;
1616
import 'package:path_provider/path_provider.dart';
1717
import 'package:timezone/data/latest_all.dart' as tz;
1818
import 'package:timezone/timezone.dart' as tz;
@@ -52,10 +52,10 @@ class ReceivedNotification {
5252
String? selectedNotificationPayload;
5353

5454
/// A notification action which triggers a url launch event
55-
const String urlLaunchActionId = 'id_1';
55+
const String urlLaunchActionId = 'id1';
5656

5757
/// A notification action which triggers a App navigation event
58-
const String navigationActionId = 'id_3';
58+
const String navigationActionId = 'id3';
5959

6060
/// Defines a iOS/MacOS notification category for text input actions.
6161
const String darwinNotificationCategoryText = 'textCategory';
@@ -1130,8 +1130,12 @@ class _HomePageState extends State<HomePage> {
11301130
linux: linuxNotificationDetails,
11311131
);
11321132
await flutterLocalNotificationsPlugin.show(
1133-
id++, 'plain title', 'plain body', notificationDetails,
1134-
payload: 'item z');
1133+
id++,
1134+
'plain title',
1135+
'plain body',
1136+
notificationDetails,
1137+
payload: 'item z',
1138+
);
11351139
}
11361140

11371141
Future<void> _showNotificationWithTextAction() async {
@@ -2748,17 +2752,20 @@ Future<void> _showLinuxNotificationWithByteDataIcon() async {
27482752
final ByteData assetIcon = await rootBundle.load(
27492753
'icons/app_icon_density.png',
27502754
);
2751-
final image.Image? iconData = image.decodePng(
2752-
assetIcon.buffer.asUint8List().toList(),
2753-
);
2754-
final Uint8List iconBytes = iconData!.getBytes();
2755+
2756+
final Uint8List iconBytes = assetIcon.buffer.asUint8List();
2757+
final ImmutableBuffer buffer = await ImmutableBuffer.fromUint8List(iconBytes);
2758+
final ImageDescriptor descriptor = await ImageDescriptor.encoded(buffer);
2759+
final int width = descriptor.width;
2760+
final int height = descriptor.height;
2761+
27552762
final LinuxNotificationDetails linuxPlatformChannelSpecifics =
27562763
LinuxNotificationDetails(
27572764
icon: ByteDataLinuxIcon(
27582765
LinuxRawIconData(
27592766
data: iconBytes,
2760-
width: iconData.width,
2761-
height: iconData.height,
2767+
width: width,
2768+
height: height,
27622769
channels: 4, // The icon has an alpha channel
27632770
hasAlpha: true,
27642771
),

flutter_local_notifications/pubspec.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ dependencies:
1414
flutter_local_notifications_platform_interface: ^7.0.0-dev.2
1515
timezone: ^0.9.0
1616

17+
dependency_overrides:
18+
flutter_local_notifications_linux:
19+
path: ../flutter_local_notifications_linux
20+
1721
dev_dependencies:
1822
flutter_driver:
1923
sdk: flutter
1.87 KB
Loading

flutter_local_notifications_linux/lib/flutter_local_notifications_linux.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// The Linux implementation of `flutter_local_notifications`.
22
library flutter_local_notifications_linux;
33

4-
// flutter_local_notifications_linux depends on dbus and posix
4+
// flutter_local_notifications_linux depends on posix
55
// which uses FFI internally; export a stub for platforms that don't
66
// support FFI (e.g., web) to avoid having transitive dependencies
77
// break web compilation.

flutter_local_notifications_linux/lib/src/dbus_wrapper.dart

Lines changed: 0 additions & 52 deletions
This file was deleted.

flutter_local_notifications_linux/lib/src/flutter_local_notifications.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,4 @@ class LinuxFlutterLocalNotificationsPlugin
7777
@override
7878
Future<LinuxServerCapabilities> getCapabilities() =>
7979
_manager.getCapabilities();
80-
81-
/// Returns a [Map] with the specified notification id as the key
82-
/// and the id, assigned by the system, as the value.
83-
///
84-
/// Note: the system ID is unique only within the current user session,
85-
/// so it's undesirable to save it to persistable storage without any
86-
/// invalidation/update. For more information, please see
87-
/// Desktop Notifications Specification https://specifications.freedesktop.org/notification-spec/latest/ar01s02.html
88-
@override
89-
Future<Map<int, int>> getSystemIdMap() => _manager.getSystemIdMap();
9080
}

flutter_local_notifications_linux/lib/src/flutter_local_notifications_platform_linux.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,4 @@ abstract class FlutterLocalNotificationsPlatformLinux
3636
/// Some functionality may not be implemented by the notification server,
3737
/// conforming clients should check if it is available before using it.
3838
Future<LinuxServerCapabilities> getCapabilities();
39-
40-
/// Returns a [Map] with the specified notification id as the key
41-
/// and the id, assigned by the system, as the value.
42-
///
43-
/// Note: the system ID is unique only within the current user session,
44-
/// so it's undesirable to save it to persistable storage without any
45-
/// invalidation/update. For more information, please see
46-
/// Desktop Notifications Specification https://specifications.freedesktop.org/notification-spec/latest/ar01s02.html
47-
Future<Map<int, int>> getSystemIdMap();
4839
}

flutter_local_notifications_linux/lib/src/flutter_local_notifications_stub.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,4 @@ class LinuxFlutterLocalNotificationsPlugin
4949
assert(false);
5050
throw UnimplementedError();
5151
}
52-
53-
/// Errors on attempted calling of the stub. It exists only to satisfy
54-
/// compile-time dependencies, and should never actually be called.
55-
@override
56-
Future<Map<int, int>> getSystemIdMap() async {
57-
assert(false);
58-
throw UnimplementedError();
59-
}
6052
}

flutter_local_notifications_linux/lib/src/helpers.dart

Lines changed: 0 additions & 61 deletions
This file was deleted.

flutter_local_notifications_linux/lib/src/notification_info.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ class LinuxNotificationInfo {
66
/// Constructs an instance of [LinuxPlatformInfoData].
77
const LinuxNotificationInfo({
88
required this.id,
9-
required this.systemId,
109
this.payload,
1110
this.actions = const <LinuxNotificationActionInfo>[],
1211
});
@@ -21,7 +20,6 @@ class LinuxNotificationInfo {
2120
.toList();
2221
return LinuxNotificationInfo(
2322
id: json['id'] as int,
24-
systemId: json['systemId'] as int,
2523
payload: json['payload'] as String?,
2624
actions: actions ?? <LinuxNotificationActionInfo>[],
2725
);
@@ -30,10 +28,6 @@ class LinuxNotificationInfo {
3028
/// Notification id
3129
final int id;
3230

33-
/// Notification id, which is returned by the system,
34-
/// see Desktop Notifications Specification https://specifications.freedesktop.org/notification-spec/latest/
35-
final int systemId;
36-
3731
/// Notification payload, that will be passed back to the app
3832
/// when a notification is tapped on.
3933
final String? payload;
@@ -44,7 +38,6 @@ class LinuxNotificationInfo {
4438
/// Returns the object as a key-value map
4539
Map<String, dynamic> toJson() => <String, dynamic>{
4640
'id': id,
47-
'systemId': systemId,
4841
'payload': payload,
4942
'actions':
5043
actions.map((LinuxNotificationActionInfo a) => a.toJson()).toList(),
@@ -60,7 +53,6 @@ class LinuxNotificationInfo {
6053
}) =>
6154
LinuxNotificationInfo(
6255
id: id ?? this.id,
63-
systemId: systemId ?? this.systemId,
6456
payload: payload ?? this.payload,
6557
actions: actions ?? this.actions,
6658
);
@@ -73,14 +65,12 @@ class LinuxNotificationInfo {
7365

7466
return other is LinuxNotificationInfo &&
7567
other.id == id &&
76-
other.systemId == systemId &&
7768
other.payload == payload &&
7869
listEquals(other.actions, actions);
7970
}
8071

8172
@override
82-
int get hashCode =>
83-
id.hashCode ^ systemId.hashCode ^ payload.hashCode ^ actions.hashCode;
73+
int get hashCode => id.hashCode ^ payload.hashCode ^ actions.hashCode;
8474
}
8575

8676
/// Represents a Linux notification action information

0 commit comments

Comments
 (0)