I am using the flutter_local_notification package to handle notifications from a third party server (not firebase cloud messaging). Because I am using firebase but not firebase messaging, I am using the onSelectNotification function of the flutter-local_notification package.
This is the function that I pass to onSelectNotification:
static _selectNotification(String payload, StreamChatClient client, RemoteMessage message) {
debugPrint('notification payload: $payload');
if(payload.contains('livestream')) {
Utils.db.getLiveRoom(payload.split(":")[1]).then((liveRoom) {
Navigator.push(
NavigationService.navigatorKey.currentContext!,
MaterialPageRoute<void>(builder: (context) => LiveRoomChat(liveRoom: liveRoom)),
);
});
}
else {
List<String> ids = message.data['channel_id'].toString().split('_');
String receiverId = '';
if(ids[0] == Utils.user?.uid) {
receiverId = ids[1];
}
else {
receiverId = ids[0];
}
Navigator.push(
NavigationService.navigatorKey.currentContext!,
MaterialPageRoute<void>(builder: (context) => MessageApi(
sourceType: Utils.friends.containsKey(receiverId) ? SourceType.friends : SourceType.justMet,
receiverId: receiverId,
channelId: payload.split(":")[1],
streamToken: Utils.user?.streamToken ?? '',
client: client
)),
);
}
}
I have a global navigator key which I have defined in a NavigationService class, and I also assign this navigator key in the main.dart. This notification handling above works for ios but it does not work for android because NavigationService.navigatorKey.currentContext is always null on android. Does anyone know why this is the case on android, and what is the way to handle it?