I think I have method that involves setting to go to the notifications tab like this:
class NotificationUtil {
static void subscribeToNotifications(BuildContext context, GlobalKey<ScaffoldState> scaffoldKey) async {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_showNotification(scaffoldKey, message);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
await PreferenceUtil.setNotificationKey();
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
await PreferenceUtil.setNotificationKey();
},
);
await _firebaseMessaging.getToken();
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
FirebaseUser firebaseUser = await AuthUtil.getFirebaseUser();
_firebaseMessaging
.subscribeToTopic(firebaseUser.email.replaceAll('@', '%'));
}
static void _showNotification(GlobalKey<ScaffoldState> scaffoldKey, Map<String, dynamic> message) {
scaffoldKey.currentState.showSnackBar(new SnackBar(
content: new Text(message['notification']['body'] as String),
));
}
}
But it looks like initState is not called when resuming from the background.
@override
void initState() {
super.initState();
PreferenceUtil.showNotifications().then((showNotification) async {
print("the show notification state is:" +showNotification.toString());
await PreferenceUtil.clearNotificationKey();
if (showNotification) {
setState(() {
_selectedIndex = 1;
});
}
});
}