Implement web notifications

This commit is contained in:
Christian Pauly 2020-06-27 10:15:37 +02:00
parent f3122ef8b7
commit e5a8bf40c8
5 changed files with 54 additions and 12 deletions

View File

@ -1,3 +1,7 @@
# Version 0.16.0 - 2020-07-??
### Features
- Implement web notifications
# Version 0.15.1 - 2020-06-26 # Version 0.15.1 - 2020-06-26
### Fixes: ### Fixes:
- Fix a big with account data being stored incorrectly - Fix a big with account data being stored incorrectly

Binary file not shown.

View File

@ -9,7 +9,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:localstorage/localstorage.dart'; import 'package:localstorage/localstorage.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:universal_html/prefer_universal/html.dart' as html;
import '../l10n/l10n.dart'; import '../l10n/l10n.dart';
import '../utils/beautify_string_extension.dart'; import '../utils/beautify_string_extension.dart';
import '../utils/famedlysdk_store.dart'; import '../utils/famedlysdk_store.dart';
@ -101,6 +101,7 @@ class MatrixState extends State<Matrix> {
StreamSubscription onRoomKeyRequestSub; StreamSubscription onRoomKeyRequestSub;
StreamSubscription onKeyVerificationRequestSub; StreamSubscription onKeyVerificationRequestSub;
StreamSubscription onJitsiCallSub; StreamSubscription onJitsiCallSub;
StreamSubscription onNotification;
void onJitsiCall(EventUpdate eventUpdate) { void onJitsiCall(EventUpdate eventUpdate) {
final event = Event.fromJson( final event = Event.fromJson(
@ -157,6 +158,28 @@ class MatrixState extends State<Matrix> {
return; return;
} }
void _showWebNotification(EventUpdate eventUpdate) async {
final room = client.getRoomById(eventUpdate.roomID);
final event = Event.fromJson(eventUpdate.content, room);
final body = event.getLocalizedBody(
L10n.of(context),
withSenderNamePrefix:
!room.isDirectChat || room.lastEvent.senderId == client.userID,
);
html.AudioElement()
..src = 'assets/assets/sounds/notification.wav'
..autoplay = true
..load();
html.Notification(
room.getLocalizedDisplayname(L10n.of(context)),
body: body,
icon: event.sender.avatarUrl?.getThumbnail(client,
width: 64, height: 64, method: ThumbnailMethod.crop) ??
room.avatar?.getThumbnail(client,
width: 64, height: 64, method: ThumbnailMethod.crop),
);
}
@override @override
void initState() { void initState() {
store = widget.store ?? Store(); store = widget.store ?? Store();
@ -234,6 +257,19 @@ class MatrixState extends State<Matrix> {
renderHtml = render == '1'; renderHtml = render == '1';
}); });
} }
if (kIsWeb) {
client.onSync.stream.first.then((s) {
html.Notification.requestPermission();
onNotification ??= client.onEvent.stream
.where((e) =>
e.roomID != activeRoomId &&
e.type == 'timeline' &&
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(e.eventType) &&
e.content['sender'] != client.userID)
.listen(_showWebNotification);
});
}
super.initState(); super.initState();
} }
@ -242,6 +278,7 @@ class MatrixState extends State<Matrix> {
onRoomKeyRequestSub?.cancel(); onRoomKeyRequestSub?.cancel();
onKeyVerificationRequestSub?.cancel(); onKeyVerificationRequestSub?.cancel();
onJitsiCallSub?.cancel(); onJitsiCallSub?.cancel();
onNotification?.cancel();
super.dispose(); super.dispose();
} }

View File

@ -157,6 +157,13 @@ packages:
url: "https://github.com/simolus3/moor.git" url: "https://github.com/simolus3/moor.git"
source: git source: git
version: "1.0.0" version: "1.0.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
famedlysdk: famedlysdk:
dependency: "direct main" dependency: "direct main"
description: description:
@ -539,7 +546,7 @@ packages:
name: path name: path
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.6.4" version: "1.7.0"
path_drawing: path_drawing:
dependency: transitive dependency: transitive
description: description:
@ -617,13 +624,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.4.2" version: "1.4.2"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
random_string: random_string:
dependency: "direct main" dependency: "direct main"
description: description:
@ -761,21 +761,21 @@ packages:
name: test name: test
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.14.4" version: "1.14.7"
test_api: test_api:
dependency: transitive dependency: transitive
description: description:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.15" version: "0.2.16"
test_core: test_core:
dependency: transitive dependency: transitive
description: description:
name: test_core name: test_core
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.3.4" version: "0.3.7"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:

View File

@ -93,6 +93,7 @@ flutter:
# To add assets to your application, add an assets section, like this: # To add assets to your application, add an assets section, like this:
assets: assets:
- assets/ - assets/
- assets/sounds/
- assets/js/ - assets/js/
- assets/js/package/ - assets/js/package/