Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a specific page on onesignal notification click on flutter?

I am using OneSignal push notification service and I want to open the app directly to specific page on notification click. I am sending the page through data. I tried navigator.push but it didn't work i guess because of context issue. I am calling _initializeonesignal() after login which contains onesignal init and the following code.

OneSignal.shared.setNotificationOpenedHandler((notification) {
  var notify = notification.notification.payload.additionalData;
  if (notify["type"] == "message") {
    //open DM(user: notify["id"])
  }
  if (notify["type"] == "user") {
   //open Profileo(notify["id"])
  }
  if (notify["type"] == "post") {
    //open ViewPost(notify["id"])
  }
  print('Opened');
});
like image 886
Ashutosh Sharma Avatar asked Oct 23 '18 16:10

Ashutosh Sharma


People also ask

How do I open a page on notification click in Flutter?

Step 1: pass one key-value pair in the firebase notification as click_action : FLUTTER_CLICK_ACTION . Step 2: Using step 1 you'll receive the onTap callback of notification inside onResume or onLaunch method. Step 3: Perform the following scenario for navigating to a specific screen on click of notification.

How do you handle notifications on Flutter?

To send a notification, go to Firebase Console → Cloud Messaging and click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token. (Enter the current FCM registration token).


2 Answers

You will need to register a global Navigator handle in your main application scaffold -- then you can use it in your notification handlers..

So -- in our app in our main App we have :

    // Initialize our global NavigatorKey
    globals.navigatorKey = GlobalKey<NavigatorState>();

...
            return MaterialApp(
              title: 'MissionMode Mobile',
              theme: theme,
              initialRoute: _initialRoute,
              onGenerateRoute: globals.router.generator,
              navigatorKey: globals.navigatorKey,
            );

The key is the navigatorKey: part and saving it to somewhere you can access somewhere else ..

Then in your handler:

OneSignal.shared.setNotificationOpenedHandler(_handleNotificationOpened); ...

// What to do when the user opens/taps on a notification
void _handleNotificationOpened(OSNotificationOpenedResult result) {
  print('[notification_service - _handleNotificationOpened()');
  print(
      "Opened notification: ${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}");

  // Since the only thing we can get current are new Alerts -- go to the Alert screen
  globals.navigatorKey.currentState.pushNamed('/home');
}

That should do the trick -- does for us anyway :)

like image 175
sjmcdowall Avatar answered Sep 19 '22 07:09

sjmcdowall


I resolved the same problems, as below:
In the main screen file MyApp.dart

@override
  void initState() {
    OneSignalWapper.handleClickNotification(context);
  }

OneSignalWapper.dart :

static void handleClickNotification(BuildContext context) {
    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) async {
      try {
        var id = await result.notification.payload.additionalData["data_id"];
        Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => PostDetailsScreen.newInstance('$id')));
      } catch (e, stacktrace) {
        log(e);
      }
    });
  }
like image 26
Mr Special Avatar answered Sep 19 '22 07:09

Mr Special