153 lines
6.7 KiB
Dart
153 lines
6.7 KiB
Dart
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:simplecloudnotifier/api/api_client.dart';
|
|
import 'package:simplecloudnotifier/main.dart';
|
|
import 'package:simplecloudnotifier/main_utils.dart';
|
|
import 'package:simplecloudnotifier/pages/channel_view/channel_view.dart';
|
|
import 'package:simplecloudnotifier/pages/message_view/message_view.dart';
|
|
import 'package:simplecloudnotifier/state/app_events.dart';
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
|
import 'package:simplecloudnotifier/state/fb_message.dart';
|
|
import 'package:simplecloudnotifier/state/globals.dart';
|
|
import 'package:simplecloudnotifier/state/app_auth.dart';
|
|
import 'package:simplecloudnotifier/state/scn_data_cache.dart';
|
|
import 'package:simplecloudnotifier/utils/navi.dart';
|
|
import 'package:simplecloudnotifier/utils/notifier.dart';
|
|
|
|
@pragma('vm:entry-point')
|
|
void notificationTapBackground(NotificationResponse notificationResponse) {
|
|
// I think only iOS triggers this, TODO
|
|
ApplicationLog.info('Received local notification<vm:entry-point>: ${notificationResponse.id}');
|
|
}
|
|
|
|
@pragma('vm:entry-point')
|
|
Future<void> onBackgroundMessage(RemoteMessage message) async {
|
|
// a firebase message was received while the app was in the background or terminated
|
|
await _receiveMessage(message, false);
|
|
}
|
|
|
|
@pragma('vm:entry-point')
|
|
void onForegroundMessage(RemoteMessage message) {
|
|
// a firebase message was received while the app was in the foreground
|
|
_receiveMessage(message, true);
|
|
}
|
|
|
|
Future<void> _receiveMessage(RemoteMessage message, bool foreground) async {
|
|
try {
|
|
// ensure globals init
|
|
if (!Globals().isInitialized) {
|
|
print('[LATE-INIT] Init Globals() - to ensure working _receiveMessage($foreground)...');
|
|
await Globals().init();
|
|
}
|
|
|
|
// ensure hive init
|
|
if (!Globals().hiveInitialized) {
|
|
print('[LATE-INIT] Init Hive - to ensure working _receiveMessage($foreground)...');
|
|
await initHive();
|
|
}
|
|
|
|
// ensure hive init
|
|
if (!Globals().hiveAdaptersRegistered) {
|
|
print('[LATE-INIT] Init Hive-Adapter - to ensure working _receiveMessage($foreground)...');
|
|
await registerHiveAdapter();
|
|
}
|
|
|
|
// ensure hive init
|
|
if (!Globals().hiveBoxesOpened) {
|
|
print('[LATE-INIT] Ensure hive boxes are open for _receiveMessage($foreground)...');
|
|
await openHiveBoxes(false);
|
|
}
|
|
} catch (exc, trace) {
|
|
ApplicationLog.writeRawFailure('Failed to init hive', {'exception': exc.toString(), 'trace': trace.toString(), 'data': message, 'foreground': foreground});
|
|
ApplicationLog.error('Failed to init hive:' + exc.toString(), trace: trace);
|
|
Notifier.showLocalNotification("", "@ERROR", "@ERROR", 'Error Channel', "Error", "Failed to receive SCN message (init failed)", null, null);
|
|
return;
|
|
}
|
|
|
|
ApplicationLog.info('Received FB message (${foreground ? 'foreground' : 'background'}): ${message.messageId ?? 'NULL'}');
|
|
|
|
String scn_msg_id;
|
|
|
|
try {
|
|
scn_msg_id = message.data['scn_msg_id'] as String;
|
|
|
|
final timestamp = DateTime.fromMillisecondsSinceEpoch(int.parse(message.data['timestamp'] as String) * 1000);
|
|
final title = message.data['title'] as String;
|
|
final channel = message.data['channel'] as String;
|
|
final channel_id = message.data['channel_id'] as String;
|
|
final body = message.data['body'] as String;
|
|
final prio = int.parse(message.data['priority'] as String);
|
|
|
|
Notifier.showLocalNotification(scn_msg_id, channel_id, channel, 'Channel: ${channel}', title, body, timestamp, prio);
|
|
} catch (exc, trace) {
|
|
ApplicationLog.writeRawFailure('Failed to decode received FB message', {'exception': exc.toString(), 'trace': trace.toString(), 'data': message, 'foreground': foreground});
|
|
ApplicationLog.error('Failed to decode received FB message' + exc.toString(), trace: trace);
|
|
Notifier.showLocalNotification("", "@ERROR", "@ERROR", 'Error Channel', "Error", "Failed to receive SCN message (decode failed)", null, null);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
FBMessageLog.insert(message);
|
|
} catch (exc, trace) {
|
|
ApplicationLog.writeRawFailure('Failed to persist received FB message', {'exception': exc.toString(), 'trace': trace.toString(), 'data': message, 'foreground': foreground});
|
|
ApplicationLog.error('Failed to persist received FB message' + exc.toString(), trace: trace);
|
|
Notifier.showLocalNotification("", "@ERROR", "@ERROR", 'Error Channel', "Error", "Failed to receive SCN message (persist failed)", null, null);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
final msg = await APIClient.getMessage(AppAuth(), scn_msg_id);
|
|
SCNDataCache().addToMessageCache([msg]);
|
|
if (foreground) AppEvents().notifyMessageReceivedListeners(msg);
|
|
} catch (exc, trace) {
|
|
ApplicationLog.writeRawFailure('Failed to query+persist message', {'exception': exc.toString(), 'trace': trace.toString(), 'data': message, 'foreground': foreground});
|
|
ApplicationLog.error('Failed to query+persist message: ' + exc.toString(), trace: trace);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void receiveLocalDarwinNotification(int id, String? title, String? body, String? payload) {
|
|
//TODO iOS?
|
|
ApplicationLog.info('Received local notification<darwin>: $id -> [$title]');
|
|
}
|
|
|
|
void receiveLocalNotification(NotificationResponse details) {
|
|
// User has tapped a flutter_local notification, while the app was running
|
|
ApplicationLog.info('Tapped local notification: [[${details.id} | ${details.actionId} | ${details.input} | ${details.notificationResponseType} | ${details.payload}]]');
|
|
|
|
handleNotificationClickAction(details.payload, Duration.zero);
|
|
}
|
|
|
|
void handleNotificationClickAction(String? payload, Duration delay) {
|
|
final parts = payload?.split('\n') ?? [];
|
|
|
|
if (parts.length == 4 && parts[0] == '@SCN_MESSAGE') {
|
|
final messageID = parts[1];
|
|
() async {
|
|
await Future.delayed(delay, () {});
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
ApplicationLog.info('Handle notification action @SCN_MESSAGE --> ${messageID}');
|
|
Navi.push(SCNApp.materialKey.currentContext!, () => MessageViewPage(messageID: messageID, preloadedData: null));
|
|
});
|
|
}();
|
|
} else if (parts.length == 3 && parts[0] == '@SCN_MESSAGE_SUMMARY') {
|
|
final channelID = parts[1];
|
|
() async {
|
|
await Future.delayed(delay, () {});
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
ApplicationLog.info('Handle notification action @SCN_MESSAGE_SUMMARY --> ${channelID}');
|
|
Navi.push(SCNApp.materialKey.currentContext!, () => ChannelViewPage(channelID: channelID, preloadedData: null, needsReload: null));
|
|
});
|
|
}();
|
|
}
|
|
}
|
|
|
|
List<DarwinNotificationCategory> getDarwinNotificationCategories() {
|
|
return <DarwinNotificationCategory>[
|
|
//TODO ?!?
|
|
];
|
|
}
|