OpenContacts/lib/clients/notification_client.dart
Nutcake 812148658d
Add iOS support (#18)
* add web support and remove all tabs except chat

* Update README.md

* feat: setup project for xcode builds + add other tabs back

* chore: update macos podfile

* chore: update packages + update build config + add audio playback on iOS/macOS + swap downloader library

* chore: remove ide-specific files

* chore: update readme

* chore: update build.gradle to reflect correct minSdkVersion

* chore: remove generated ic_launcher png files

* chore: update formatting to use 120 char line length

* chore: address use of then method on awaited future

* chore: update app icons for macOS & iOS

* Fix file move not waiting for download finish

---------

Co-authored-by: lumey <lumey@lumey.dev>
Co-authored-by: Garrett <toast@isota.ch>
2023-11-14 20:28:14 +01:00

96 lines
3.2 KiB
Dart

import 'dart:convert';
import 'package:collection/collection.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart' as fln;
import 'package:recon/auxiliary.dart';
import 'package:recon/models/message.dart';
import 'package:recon/models/session.dart';
class NotificationChannel {
final String id;
final String name;
final String description;
const NotificationChannel({required this.name, required this.id, required this.description});
}
class NotificationClient {
static const NotificationChannel _messageChannel = NotificationChannel(
id: "messages",
name: "Messages",
description: "Messages received from your friends",
);
final fln.FlutterLocalNotificationsPlugin _notifier = fln.FlutterLocalNotificationsPlugin()
..initialize(const fln.InitializationSettings(
android: fln.AndroidInitializationSettings("ic_notification"),
iOS: fln.DarwinInitializationSettings(),
macOS: fln.DarwinInitializationSettings(),
linux: fln.LinuxInitializationSettings(defaultActionName: "Open ReCon"),
));
Future<void> showUnreadMessagesNotification(Iterable<Message> messages) async {
if (messages.isEmpty) return;
final bySender = groupBy(messages, (p0) => p0.senderId);
for (final entry in bySender.entries) {
final uname = entry.key.stripUid();
await _notifier.show(
uname.hashCode,
null,
null,
fln.NotificationDetails(
android: fln.AndroidNotificationDetails(
_messageChannel.id,
_messageChannel.name,
channelDescription: _messageChannel.description,
importance: fln.Importance.high,
priority: fln.Priority.max,
actions: [], //TODO: Make clicking message notification open chat of specified user.
styleInformation: fln.MessagingStyleInformation(
fln.Person(
name: uname,
bot: false,
),
groupConversation: false,
messages: entry.value.map((message) {
String content;
switch (message.type) {
case MessageType.unknown:
content = "Unknown Message Type";
break;
case MessageType.text:
content = message.content;
break;
case MessageType.sound:
content = "Audio Message";
break;
case MessageType.sessionInvite:
try {
final session = Session.fromMap(jsonDecode(message.content));
content = "Session Invite to ${session.name}";
} catch (e) {
content = "Session Invite";
}
break;
case MessageType.object:
content = "Asset";
break;
}
return fln.Message(
content,
message.sendTime.toLocal(),
fln.Person(
name: uname,
bot: false,
),
);
}).toList(),
),
),
),
);
}
}
}