import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:stomp_dart_client/stomp_dart_client.dart'; const socketUrl = "wss://server.muaawana.com/ws/websocket"; class NotificationSocket extends ChangeNotifier { StompClient? stompClient; FlutterLocalNotificationsPlugin notifications = new FlutterLocalNotificationsPlugin(); String token = "InvalidToken"; int statusCode = -1; bool errorToken = false; Future getToken() async { final prefs = await SharedPreferences.getInstance(); if (prefs.getString('token') == null || prefs.getString('token') == '') { prefs.setString('token', 'InvalidToken'); } if (prefs.getString('token') != null) { token = prefs.getString('token')!; } return token.toString(); } void onConnect(StompFrame frame) { if (stompClient!.connected //&& userInfo.isValidToken ) { //print('connected notification socket'); stompClient!.subscribe( destination: "/chat", callback: (frame) { // onNotification(frame); }, ); } } Future connectSock() async { if (stompClient == null) { stompClient = StompClient( config: StompConfig( url: socketUrl, onConnect: onConnect, stompConnectHeaders: { 'Authorization': 'Bearer $token', }, onWebSocketError: (dynamic error) => log("NotificationSocket --> connectSock ---> onWebSocketError $error"), onStompError: (dynamic error) => log("onStompError $error"), )); stompClient!.activate(); var androidInitialize = const AndroidInitializationSettings('@mipmap/ic_launcher'); var iOSInitialize = const DarwinInitializationSettings(); var initializeSettings = InitializationSettings( android: androidInitialize, iOS: iOSInitialize); notifications.initialize(initializeSettings); } notifyListeners(); } // this only for meeting notification // chat notification is on the chat_socket.dart // onNotification(StompFrame frame){ // var iosChannel = IOSNotificationDetails(); // var androidChannel = AndroidNotificationDetails( // 'channel-id', 'channel-name', // importance: Importance.max, // priority: Priority.max, // ); // var platformChannel = NotificationDetails(android: androidChannel, iOS: iosChannel); // final result = jsonDecode(frame.body); // // UserModel company = new UserModel(); // UserModel user = new UserModel(); // company = UserModel.fromJson(result['company']); // user = UserModel.fromJson(result['user']); // MeetingModel meeting = new MeetingModel( // id: result['id'], // date: result['date'], // time: result['timetime'], // durationmunits: result['durationmunits'], // roomid: result['roomid'], // user: user, // company: company, // subject: result['subject'], // state: result['state'], // companystate: result['companystate'], // userstate: result['userstate'], // reason: result['reason']==null?null:result['reason'], // timestmp: result['timestmp'], // month: result['month'], // day: result['day'], // year: result['year'], // ); // // NotificationsModel notification = NotificationsModel( // sender: company, // recipient: user, // chat: null, // meeting: meeting, // timestmp: result['timestmp'], // ); // notifications.show(0, company.name, meeting.companystate=="ACCEPTED"?"Invites you for a meeting.":"Joined the meeting.", platformChannel); // // notificationsProvider.addNotification(notification); // //print('notification added - meeting'); // } }