<commit_before>import 'dart:io';

import 'package:campus_mobile_experimental/app_constants.dart';
import 'package:campus_mobile_experimental/core/models/topics.dart';
import 'package:campus_mobile_experimental/core/providers/bottom_nav.dart';
import 'package:campus_mobile_experimental/core/providers/messages.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:campus_mobile_experimental/core/services/notifications.dart';
import 'package:device_info/device_info.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class PushNotificationDataProvider extends ChangeNotifier {
  PushNotificationDataProvider() {
    ///INITIALIZE SERVICES
    _notificationService = NotificationService();
    deviceInfoPlugin = DeviceInfoPlugin();
    _fcm = FirebaseMessaging.instance;
    initState();
  }

  ///Models
  FirebaseMessaging _fcm;
  DeviceInfoPlugin deviceInfoPlugin;
  Map<String, dynamic> _deviceData = <String, dynamic>{};

  ///STATES
  DateTime _lastUpdated;
  String _error;
  List<TopicsModel> _topicsModel;
  Map<String, bool> _topicSubscriptionState = <String, bool>{};

  ///SERVICES
  NotificationService _notificationService;

  /// invokes correct method to receive device info
  /// invokes [fetchTopicsList]
  initState() async {
    fetchTopicsList();
    //onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    if (Platform.isAndroid) {
      _deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
    } else if (Platform.isIOS) {
      _deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);
      // Handled automatically by getToken
      // _fcm.requestPermission(const IosNotificationSettings(
      //     sound: true, badge: true, alert: true, provisional: true));
      // _fcm.onIosSettingsRegistered
      //     .listen((IosNotificationSettings settings) {});
    }

    ///listen for token changes and register user
    _fcm.onTokenRefresh.listen(
      (token) {
        print('fcm: onTokenRefresh: ' + token);
        registerDevice(token);
      },
    );
  }

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  /// Configures the [_fcm] object to receive push notifications
  Future<void> initPlatformState(BuildContext context) async {
    try {
      const AndroidInitializationSettings initializationSettingsAndroid =
          AndroidInitializationSettings('mipmap/ic_launcher_round');
      final IOSInitializationSettings initializationSettingsIOS =
          IOSInitializationSettings();
      final InitializationSettings initializationSettings =
          InitializationSettings(
        android: initializationSettingsAndroid,
        iOS: initializationSettingsIOS,
      );
      await flutterLocalNotificationsPlugin.initialize(initializationSettings);

      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        /// foreground messaging via flutter_local_notifications

        showNotification(message);
        print('FCM: onMessage: foreground message:');
        print(message.data);
        print("Body: " + message.notification.body);
        print(message.notification.title);
        print(message);

        /// Fetch in-app messages
        Provider.of<MessagesDataProvider>(context, listen: false)
            .fetchMessages(true);

        /// Set tab bar index to the Notifications tab
        Provider.of<BottomNavigationBarProvider>(context, listen: false)
            .currentIndex = NavigatorConstants.NotificationsTab;

        /// Navigate to Notifications tab
        Navigator.of(context).pushNamedAndRemoveUntil(
            RoutePaths.BottomNavigationBar, (Route<dynamic> route) => false);
      });

      FirebaseMessaging.onMessageOpenedApp.listen(
        (RemoteMessage message) {
          print('FCM: onMessageOpenedApp: background message:');
          print(message);

          /// Fetch in-app messages
          Provider.of<MessagesDataProvider>(context, listen: false)
              .fetchMessages(true);

          /// Set tab bar index to the Notifications tab
          Provider.of<BottomNavigationBarProvider>(context, listen: false)
              .currentIndex = NavigatorConstants.NotificationsTab;

          /// Navigate to Notifications tab
          Navigator.of(context).pushNamedAndRemoveUntil(
              RoutePaths.BottomNavigationBar, (Route<dynamic> route) => false);
        },<commit_msg>removed navigation to fix auto navigate but it didnt work<commit_after>import 'dart:io';

import 'package:campus_mobile_experimental/app_constants.dart';
import 'package:campus_mobile_experimental/core/models/topics.dart';
import 'package:campus_mobile_experimental/core/providers/bottom_nav.dart';
import 'package:campus_mobile_experimental/core/providers/messages.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:campus_mobile_experimental/core/services/notifications.dart';
import 'package:device_info/device_info.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class PushNotificationDataProvider extends ChangeNotifier {
  PushNotificationDataProvider() {
    ///INITIALIZE SERVICES
    _notificationService = NotificationService();
    deviceInfoPlugin = DeviceInfoPlugin();
    _fcm = FirebaseMessaging.instance;
    initState();
  }

  ///Models
  FirebaseMessaging _fcm;
  DeviceInfoPlugin deviceInfoPlugin;
  Map<String, dynamic> _deviceData = <String, dynamic>{};

  ///STATES
  DateTime _lastUpdated;
  String _error;
  List<TopicsModel> _topicsModel;
  Map<String, bool> _topicSubscriptionState = <String, bool>{};

  ///SERVICES
  NotificationService _notificationService;

  /// invokes correct method to receive device info
  /// invokes [fetchTopicsList]
  initState() async {
    fetchTopicsList();
    //onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    if (Platform.isAndroid) {
      _deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
    } else if (Platform.isIOS) {
      _deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);
      // Handled automatically by getToken
      // _fcm.requestPermission(const IosNotificationSettings(
      //     sound: true, badge: true, alert: true, provisional: true));
      // _fcm.onIosSettingsRegistered
      //     .listen((IosNotificationSettings settings) {});
    }

    ///listen for token changes and register user
    _fcm.onTokenRefresh.listen(
      (token) {
        print('fcm: onTokenRefresh: ' + token);
        registerDevice(token);
      },
    );
  }

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

  /// Configures the [_fcm] object to receive push notifications
  Future<void> initPlatformState(BuildContext context) async {
    try {
      const AndroidInitializationSettings initializationSettingsAndroid =
          AndroidInitializationSettings('mipmap/ic_launcher_round');
      final IOSInitializationSettings initializationSettingsIOS =
          IOSInitializationSettings();
      final InitializationSettings initializationSettings =
          InitializationSettings(
        android: initializationSettingsAndroid,
        iOS: initializationSettingsIOS,
      );
      await flutterLocalNotificationsPlugin.initialize(initializationSettings);

      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        /// foreground messaging via flutter_local_notifications

        showNotification(message);
        print('FCM: onMessage: foreground message:');
        print(message.data);
        print("Body: " + message.notification.body);
        print(message.notification.title);
        print(message);

        /// Fetch in-app messages
        Provider.of<MessagesDataProvider>(context, listen: false)
            .fetchMessages(true);

        /// Set tab bar index to the Notifications tab
        Provider.of<BottomNavigationBarProvider>(context, listen: false)
            .currentIndex = NavigatorConstants.NotificationsTab;
      });

      FirebaseMessaging.onMessageOpenedApp.listen(
        (RemoteMessage message) {
          print('FCM: onMessageOpenedApp: background message:');
          print(message);

          /// Fetch in-app messages
          Provider.of<MessagesDataProvider>(context, listen: false)
              .fetchMessages(true);

          /// Set tab bar index to the Notifications tab
          Provider.of<BottomNavigationBarProvider>(context, listen: false)
              .currentIndex = NavigatorConstants.NotificationsTab;

          /// Navigate to Notifications tab
          Navigator.of(context).pushNamedAndRemoveUntil(
              RoutePaths.BottomNavigationBar, (Route<dynamic> route) => false);
        },
{'fraction_non_alphanumeric': 0.09361329833770779, 'fraction_numerical': 0.0, 'mean_word_length': 3.718782249742002, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5379555', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2373, 'n_words': 472}