Skip to content

Commit c6d8624

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 4d8aa14 commit c6d8624

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';
@@ -1153,8 +1153,12 @@ class _HomePageState extends State<HomePage> {
11531153
linux: linuxNotificationDetails,
11541154
);
11551155
await flutterLocalNotificationsPlugin.show(
1156-
id++, 'plain title', 'plain body', notificationDetails,
1157-
payload: 'item z');
1156+
id++,
1157+
'plain title',
1158+
'plain body',
1159+
notificationDetails,
1160+
payload: 'item z',
1161+
);
11581162
}
11591163

11601164
Future<void> _showNotificationWithTextAction() async {
@@ -2831,17 +2835,20 @@ Future<void> _showLinuxNotificationWithByteDataIcon() async {
28312835
final ByteData assetIcon = await rootBundle.load(
28322836
'icons/app_icon_density.png',
28332837
);
2834-
final image.Image? iconData = image.decodePng(
2835-
assetIcon.buffer.asUint8List().toList(),
2836-
);
2837-
final Uint8List iconBytes = iconData!.getBytes();
2838+
2839+
final Uint8List iconBytes = assetIcon.buffer.asUint8List();
2840+
final ImmutableBuffer buffer = await ImmutableBuffer.fromUint8List(iconBytes);
2841+
final ImageDescriptor descriptor = await ImageDescriptor.encoded(buffer);
2842+
final int width = descriptor.width;
2843+
final int height = descriptor.height;
2844+
28382845
final LinuxNotificationDetails linuxPlatformChannelSpecifics =
28392846
LinuxNotificationDetails(
28402847
icon: ByteDataLinuxIcon(
28412848
LinuxRawIconData(
28422849
data: iconBytes,
2843-
width: iconData.width,
2844-
height: iconData.height,
2850+
width: width,
2851+
height: height,
28452852
channels: 4, // The icon has an alpha channel
28462853
hasAlpha: true,
28472854
),

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
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)