forked from Nexus/nexus
send notifications natively
This commit is contained in:
parent
11d83ee067
commit
2beb6dcb9b
14 changed files with 225 additions and 30 deletions
|
|
@ -3,9 +3,12 @@ import "dart:io";
|
|||
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:intl/intl.dart";
|
||||
import "package:nexus/controllers/notifications.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/client_state.dart";
|
||||
import "package:nexus/models/content/message.dart";
|
||||
import "package:nexus/models/requests/register_pusher.dart";
|
||||
import "package:unifiedpush/unifiedpush.dart";
|
||||
import "package:unifiedpush_storage_shared_preferences/storage.dart";
|
||||
|
|
@ -38,10 +41,29 @@ class UnifiedPushController extends AsyncNotifier<bool> {
|
|||
);
|
||||
},
|
||||
onMessage: (message, instance) async {
|
||||
if (message.decrypted == false) {
|
||||
// TODO: Handle more gracefully, show option to re-register pusher
|
||||
throw Exception("Failed to decrypt notification");
|
||||
}
|
||||
final event = await client.handlePush(
|
||||
json.decode(String.fromCharCodes(message.content)),
|
||||
);
|
||||
print(event);
|
||||
|
||||
final room = ref.read(
|
||||
RoomsController.provider.select((rooms) => rooms[event.roomId]),
|
||||
);
|
||||
|
||||
await ref
|
||||
.read(NotificationsController.provider.notifier)
|
||||
.send(
|
||||
id: event.eventId.hashCode & 0x7fffffff,
|
||||
title: room?.metadata?.name ?? "New Message",
|
||||
body: switch (event.content) {
|
||||
MessageContent(:final body) => body,
|
||||
// TODO: Handle more types
|
||||
_ => null,
|
||||
},
|
||||
);
|
||||
},
|
||||
onRegistrationFailed: (error, instance) => throw error,
|
||||
onUnregistered: (instance) => ref.invalidateSelf(),
|
||||
|
|
|
|||
Loading…
Reference in a new issue