Skip to content

Commit

Permalink
redirect to the chat when click on a notification (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin authored Nov 19, 2024
2 parents e502882 + d59d4d4 commit 34159d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
24 changes: 19 additions & 5 deletions app/lib/pages/home/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ import 'package:upgrader/upgrader.dart';
import 'widgets/battery_info_widget.dart';

class HomePageWrapper extends StatefulWidget {
const HomePageWrapper({super.key});
final bool openAppFromNotification;
const HomePageWrapper({super.key, this.openAppFromNotification = false});

@override
State<HomePageWrapper> createState() => _HomePageWrapperState();
}

class _HomePageWrapperState extends State<HomePageWrapper> {
late bool _openAppFromNotification;
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
Expand All @@ -63,17 +65,19 @@ class _HomePageWrapperState extends State<HomePageWrapper> {
context.read<AppProvider>().setSelectedChatAppId(null);
}
});
_openAppFromNotification = widget.openAppFromNotification;
super.initState();
}

@override
Widget build(BuildContext context) {
return const HomePage();
return HomePage(openAppFromNotification: _openAppFromNotification);
}
}

class HomePage extends StatefulWidget {
const HomePage({super.key});
final bool openAppFromNotification;
const HomePage({super.key, this.openAppFromNotification = false});

@override
State<HomePage> createState() => _HomePageState();
Expand Down Expand Up @@ -132,9 +136,19 @@ class _HomePageState extends State<HomePage> with WidgetsBindingObserver, Ticker

@override
void initState() {
SharedPreferencesUtil().pageToShowFromNotification = 0; // TODO: whatisit
SharedPreferencesUtil().onboardingCompleted = true;
_controller = PageController();
if (widget.openAppFromNotification) {
context.read<HomeProvider>().selectedIndex = SharedPreferencesUtil().pageToShowFromNotification;
_controller = PageController(initialPage: SharedPreferencesUtil().pageToShowFromNotification);
if (SharedPreferencesUtil().pageToShowFromNotification == 1) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await context.read<MessageProvider>().refreshMessages();
});
}
SharedPreferencesUtil().pageToShowFromNotification = 0;
} else {
_controller = PageController();
}
WidgetsBinding.instance.addObserver(this);

WidgetsBinding.instance.addPostFrameCallback((_) async {
Expand Down
25 changes: 13 additions & 12 deletions app/lib/services/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ class NotificationService {
void _handleOnTap(RemoteMessage message) {
final data = message.data;
if (data.isNotEmpty) {
if (message.data['notification_type'] == 'daily_summary') {
if (message.data['notification_type'] == 'daily_summary' || message.data['notification_type'] == 'plugin') {
SharedPreferencesUtil().pageToShowFromNotification = 1;
MyApp.navigatorKey.currentState
?.pushReplacement(MaterialPageRoute(builder: (context) => const HomePageWrapper()));
?.pushReplacement(MaterialPageRoute(builder: (context) => const HomePageWrapper(openAppFromNotification: true)));
}
}
}
Expand All @@ -187,15 +187,16 @@ class NotificationService {

// Plugin
if (data.isNotEmpty) {
if (noti != null) {
_showForegroundNotification(noti: noti);
}

late Map<String, String> payload = <String, String>{};
final notificationType = data['notification_type'];
if (notificationType == 'plugin' || notificationType == 'daily_summary') {
data['from_integration'] = data['from_integration'] == 'true';
payload.addAll({'path':'/chat'});
_serverMessageStreamController.add(ServerMessage.fromJson(data));
}
if (noti != null) {
_showForegroundNotification(noti: noti, payload: payload);
}
return;
}

Expand All @@ -212,9 +213,9 @@ class NotificationService {
Stream<ServerMessage> get listenForServerMessages => _serverMessageStreamController.stream;

Future<void> _showForegroundNotification(
{required RemoteNotification noti, NotificationLayout layout = NotificationLayout.Default}) async {
{required RemoteNotification noti, NotificationLayout layout = NotificationLayout.Default, Map<String, String?>? payload}) async {
final id = Random().nextInt(10000);
showNotification(id: id, title: noti.title!, body: noti.body!, layout: layout);
showNotification(id: id, title: noti.title!, body: noti.body!, layout: layout, payload: payload);
}
}

Expand Down Expand Up @@ -257,8 +258,8 @@ class NotificationUtil {

static Future<void> onActionReceivedMethodImpl(ReceivedAction receivedAction) async {
final Map<String, int> screensWithRespectToPath = {
'/chat': 2,
'/capture': 1,
'/apps': 2,
'/chat': 1,
'/memories': 0,
};
var message = 'Action ${receivedAction.actionType?.name} received on ${receivedAction.actionLifeCycle?.name}';
Expand All @@ -271,7 +272,7 @@ class NotificationUtil {
if (payload?.containsKey('navigateTo') ?? false) {
SharedPreferencesUtil().subPageToShowFromNotification = payload?['navigateTo'] ?? '';
}
SharedPreferencesUtil().pageToShowFromNotification = screensWithRespectToPath[payload?['path']] ?? 1;
MyApp.navigatorKey.currentState?.pushReplacement(MaterialPageRoute(builder: (context) => const HomePageWrapper()));
SharedPreferencesUtil().pageToShowFromNotification = screensWithRespectToPath[payload?['path']] ?? 0;
MyApp.navigatorKey.currentState?.pushReplacement(MaterialPageRoute(builder: (context) => const HomePageWrapper(openAppFromNotification: true)));
}
}

0 comments on commit 34159d6

Please sign in to comment.