Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Push Notification click redirection to specific screen not working

Tags:

flutter

I have use flutter_local_notifications, push notification is successfully receiving and when i click on it, i'm trying to redirect to specific screen, but it always redirect to home screen.

Below is my code.

@override
void initState() {
  // TODO: implement initState
  super.initState();

  // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
  var initializationSettingsAndroid =
      new AndroidInitializationSettings('app_icon');
  var initializationSettingsIOS = new IOSInitializationSettings();
  var initializationSettings = new InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIOS);
  flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: onSelectNotification);

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      print('on message $message');

      var data = message['data'] as Map;
      var msg1 = data['message'] as String;
      var response = json.decode(msg1) as Map;

      String str_title = response['title'] as String;
      String str_body = response['body'] as String;

      _showNotification(str_title, str_body, msg1);
    },
    onResume: (Map<String, dynamic> message) {
      print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) {
      print('on launch $message');
}

_showNotification function code

void _showNotification(String title, String body, String pay_load) async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    AppConstant.notify_channel_id,
    AppConstant.notify_channel_name,
    AppConstant.notify_channel_desc,
    importance: Importance.Max,
    priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin
    .show(0, title, body, platformChannelSpecifics, payload: pay_load);
}

onSelectNotification function code

Future onSelectNotification(String payload) async {
  if (payload != null) {
    debugPrint('notification payload: ' + payload);
  }

  // here set and put condition for property id
  var response = json.decode(payload) as Map;

  Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) =>
            PropertyDetailScreen()),
);

PropertyDetailScreen is the screen where i want to redirect when click on notification, but it is always redirect to home screen, So please guide me where i am wrong, or why my code its not working.

like image 735
Nirav Bhavsar Avatar asked Dec 26 '18 14:12

Nirav Bhavsar


People also ask

How do you direct push notifications to a specific page 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 send a push notification to a specific user 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.

How do you handle local notification click in Flutter?

In Flutter, it's best practice to seclude your logic from your UI. To do this, we'll create a class called NotificationService in the notification_service. dart file. This class will handle all the notification logic and expose methods to create, send, schedule, and cancel notifications.


1 Answers

Try by using GlobalKey as following

Declare it inside

class _MyAppState extends State<MyApp> {
    final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "MainNavigator");
    }

Assign it in MaterialApp

MaterialApp(
              navigatorKey: navigatorKey,
              supportedLocales: [
                Locale('en', 'US'),
                Locale('ar', ''),
                Locale('it'),
                Locale('fr'),
                Locale('es'),
                Locale('de'),
                Locale('pt'),
              ],
    );

Use it as navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => YourClassName()));

like image 159
Vajani Kishan Avatar answered Oct 02 '22 10:10

Vajani Kishan