forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.dart
More file actions
139 lines (117 loc) · 4.93 KB
/
Copy pathnotifications.dart
File metadata and controls
139 lines (117 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dart:async';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:omi/app_globals.dart';
import 'package:omi/pages/home/page.dart';
import 'package:omi/utils/l10n_extensions.dart';
import 'package:omi/utils/logger.dart';
// Re-export the main notification service for backward compatibility
// All notification functionality is now handled by the platform-aware service
export 'package:omi/services/notifications/notification_service.dart';
class NotificationUtil {
static ReceivePort? receivePort;
static Future<void> initializeNotificationsEventListeners() async {
// Only after at least the action method is set, the notification events are delivered
AwesomeNotifications().setListeners(onActionReceivedMethod: NotificationUtil.onActionReceivedMethod);
}
static Future<void> initializeIsolateReceivePort() async {
receivePort = ReceivePort('Notification action port in main isolate');
receivePort!.listen((serializedData) {
final receivedAction = ReceivedAction().fromMap(serializedData);
onActionReceivedMethodImpl(receivedAction);
});
// This initialization only happens on main isolate
IsolateNameServer.registerPortWithName(receivePort!.sendPort, 'notification_action_port');
}
/// Use this method to detect when the user taps on a notification or action button
@pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(ReceivedAction receivedAction) async {
if (receivePort != null) {
await onActionReceivedMethodImpl(receivedAction);
} else {
print(
'onActionReceivedMethod was called inside a parallel dart isolate, where receivePort was never initialized.',
);
SendPort? sendPort = IsolateNameServer.lookupPortByName('notification_action_port');
if (sendPort != null) {
print('Redirecting the execution to main isolate process in listening...');
dynamic serializedData = receivedAction.toMap();
sendPort.send(serializedData);
}
}
}
static Future<void> onActionReceivedMethodImpl(ReceivedAction receivedAction) async {
if (receivedAction.payload == null || receivedAction.payload!.isEmpty) {
return;
}
await _handleAppLinkOrDeepLink(receivedAction.payload!);
}
/// Public entry for FCM background/terminated notification taps (#5126).
static void handleNavigateTo(String route) {
// Fire-and-forget: waits for navigator readiness before pushing (terminated cold start).
unawaited(_handleAppLinkOrDeepLink({'navigate_to': route}));
}
/// Extract a chat/conversation deep-link from an FCM data map.
/// Returns null when `navigate_to` is missing, empty, or not a String.
static String? navigateToFromFcmData(Map<String, dynamic> data) {
final navigateTo = data['navigate_to'];
if (navigateTo is String && navigateTo.isNotEmpty) {
return navigateTo;
}
return null;
}
/// Poll until [read] returns non-null, or [timeout] elapses.
///
/// Used so terminated-state FCM taps (`getInitialMessage`) that resolve before
/// `runApp` attach the navigator do not silently no-op (#5126; same class of
/// race as app_links cold-start in `app_shell` / #4763).
@visibleForTesting
static Future<T?> waitUntilNonNull<T>(
T? Function() read, {
Duration timeout = const Duration(seconds: 15),
Duration pollInterval = const Duration(milliseconds: 16),
}) async {
final existing = read();
if (existing != null) return existing;
final deadline = DateTime.now().add(timeout);
while (DateTime.now().isBefore(deadline)) {
await Future<void>.delayed(pollInterval);
final value = read();
if (value != null) return value;
}
return null;
}
static Future<void> _handleAppLinkOrDeepLink(Map<String, dynamic> payload) async {
WidgetsFlutterBinding.ensureInitialized();
final navigateTo = payload['navigate_to'];
if (navigateTo is! String || navigateTo.isEmpty) {
Logger.debug('Navigate To is null');
return;
}
final navigator = await waitUntilNonNull(() => globalNavigatorKey.currentState);
if (navigator == null) {
Logger.debug('Navigator unavailable; dropping navigate_to=$navigateTo');
return;
}
navigator.pushReplacement(
MaterialPageRoute(builder: (context) => HomePageWrapper(navigateToRoute: navigateTo)),
);
}
static Future<void> triggerFallNotification() async {
final allowed = await AwesomeNotifications().isNotificationAllowed();
if (!allowed) return;
final ctx = globalNavigatorKey.currentContext;
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 6,
channelKey: 'channel',
actionType: ActionType.Default,
title: ctx?.l10n.fallNotificationTitle ?? 'Ouch',
body: ctx?.l10n.fallNotificationBody ?? 'Did you fall?',
wakeUpScreen: true,
),
);
}
}