In Flutter awesome notification, how to direct the pages when the notification is clicked

206 views Asked by At

I am developing a flutter app using awesome notification. Now I want to direct the navigation to a specific page when a notification is clicked.

I tried many things but still it didn't work. The page I want to navigate is Notification_page which is currently stateless. I also want to display that notification in that page also.

1

There are 1 answers

0
Tanishq Chawda On

Well I am Using flutter_local_notification

Here My Code

   @pragma('vm:entry-point')
Future<void> notificationTapBackground(NotificationResponse notificationResponse) async {
  await Firebase.initializeApp();

  // handle action
  await handleNotificationClick(notificationResponse.payload!);

  print('Handling a background message');

  // showToast("${notificationResponse.payload}");
}

handleNotificationClick(String dataStr) async {
  Map<String, dynamic> map = jsonDecode(dataStr);

  if (map["notificationType"].toString().compareTo(NotificationType.StockSell.name.toString()) == 0 || map["notificationType"].toString().compareTo(NotificationType.StockBuy.name.toString()) == 0) {
    Navigator.push(navState.currentContext!,
        CupertinoPageRoute(builder: (context) => const TransactionDetailPage(), settings: RouteSettings(arguments: NotificationArguments(false, map["id"].toString(), map["notificationId"].toString()))));
  } else if (map["notificationType"].toString().compareTo(NotificationType.RecommendationCreated.name.toString()) == 0) {
    if (HydratedBloc.storage.read(Constant.USER_LOGIN) == true) {
      Navigator.push(navState.currentContext!,
          CupertinoPageRoute(builder: (context) => const NotificationsPage(), settings: RouteSettings(arguments: NotificationArguments(false, map["id"].toString(), map["notificationId"].toString()))));
    } else {
      Navigator.pushReplacement(navState.currentContext!, CupertinoPageRoute(builder: (context) => const DashboardPage2()));
    }
  }
}

class NotificationUtils {
  static final _notifications = FlutterLocalNotificationsPlugin();

  static Future init() async {
    AndroidInitializationSettings android = const AndroidInitializationSettings('@mipmap/ic_launcher');

    final DarwinInitializationSettings ios = DarwinInitializationSettings(
        requestSoundPermission: true,
        requestBadgePermission: true,
        requestAlertPermission: true,
        defaultPresentSound: true,
        defaultPresentAlert: true,
        defaultPresentBadge: true,
        onDidReceiveLocalNotification: (a, b, c, d) {
          print("onDidReceiveLocalNotification");
        });

    InitializationSettings settings = InitializationSettings(android: android, iOS: ios);

    _notifications.initialize(
      settings,
      onDidReceiveNotificationResponse: (NotificationResponse notificationResponse) async {
        handleNotificationClick(notificationResponse.payload!);
      },
      onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
    );

    final bool? result = await _notifications.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(
          alert: true,
          badge: true,
          sound: true,
        );
  }

  static Future showNotification({required int id, required String notificationType, required String title, required String body, required String payload}) async => _notifications.show(
        id,
        title,
        body,
        await _notificationDetails(notificationType),
        payload: payload,
      );

  static Future _notificationDetails(String notificationType) async {
    AndroidNotificationDetails androidPlatformChannelSpecifics = const AndroidNotificationDetails(
      'high_importance_channel', // id
      'High Importance Notifications', // title
      channelDescription: 'This channel is used for important notifications.',
      // description
      importance: Importance.max,
      priority: Priority.high,
      sound: RawResourceAndroidNotificationSound('slow_spring_board'),
      playSound: true,
      visibility: NotificationVisibility.public,
      // vibrationPattern: vibrationPattern,
      enableLights: true,
      enableVibration: true,
    );

    await _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(const AndroidNotificationChannel(
          'high_importance_channel', // id
          'High Importance Notifications', // title
          description: 'This channel is used for important notifications.', // description
          importance: Importance.high,
        ));

    DarwinNotificationDetails iOSPlatformChannelSpecifics = const DarwinNotificationDetails(sound: 'slow_spring_board.aiff', presentSound: true, presentAlert: true, presentBadge: true);

    return NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );
  }
}

and this Class I have initialised in Main.dart NotificationUtils.init();

Well Same implementation process in awesome_notification as I know just look updated Doc of Awesome Notification and I hope my answer will give you the Context for the implementation

Thankyou!! Happy Coding