Bloc: flutter_bloc - Notification 0nResume redirect to new page

Created on 19 Jul 2020  路  5Comments  路  Source: felangel/bloc

Hi,

I am using flutter_bloc in my application and for notifications, I am using https://pub.dev/packages/firebase_messaging. I am in need of redirecting the user to a specific screen in the application when a notification is received via onResume() or onLauch() method in the main.dart. How to do this?

When tried with Navigator, page redirection is not happening. I am following https://github.com/felangel/bloc/tree/master/examples/flutter_login for this.

Please help me to resolve this issue.

Thanks.

question

Most helpful comment

@mintymac thanks so much for taking the time to answer, I really appreciate it!

@karthikeyanp307 closing this for now but feel free to comment with any additional questions and I鈥檓 happy to continue the conversation 馃憤

All 5 comments

@karthikeyanp307 Can you share your project or some code with where the issue is happening.

It would seem like your bloc isn't high enough in the tree to catch when a notification comes in to yield a new state OR that your state isn't changing correctly and so the Navigation code isn't being called.

Of course, it could be neither of these things.

HI @karthikeyanp307 馃憢
Thanks for opening an issue!

As @warriorCoder mentioned, it would be much easier to help if you could share a sample app which illustrates the problem you're facing 馃憤

Hi @felangel ,

Thanks for your reply and comments. I have shared a sample code via https://github.com/karthikeyanp307/flutter_login. This is not an executable one but for your view. Please help me to redirect to the inbox page from main.dart.

Thanks in advance.

@karthikeyanp307 To do this, you need to do something like the following in initState():

_firebaseMessaging.configure(onResume:
(Map message) async {
print("onResume: $message");
print(notificationBloc.state.toString());
notificationBloc.add(
NotificationReceived(
my.Notification(
id: uuid.v5(Uuid.NAMESPACE_URL, 'www.example.com'),
title: message['notification']['title'],
body: message['notification']['body']),
),
);
},
);

Then to redirect your page, you need to listen to the bloc state with a bloc consumer in your navigation element.

To recap ->

  • onResume() code is run on app resume
  • event is triggered from the message
  • bloc emits state
  • bloc consumer triggers navigator to redirect to a path

@mintymac thanks so much for taking the time to answer, I really appreciate it!

@karthikeyanp307 closing this for now but feel free to comment with any additional questions and I鈥檓 happy to continue the conversation 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinvp picture abinvp  路  3Comments

timtraversy picture timtraversy  路  3Comments

nerder picture nerder  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

ricktotec picture ricktotec  路  3Comments