|
|
|
|
@ -1,10 +1,151 @@
|
|
|
|
|
// import 'dart:io';
|
|
|
|
|
//
|
|
|
|
|
// import 'package:firebase_core/firebase_core.dart';
|
|
|
|
|
// import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
// import 'package:flutter/foundation.dart';
|
|
|
|
|
// import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
|
// import 'package:huawei_hmsavailability/huawei_hmsavailability.dart';
|
|
|
|
|
// import 'package:huawei_push/huawei_push.dart' as huawei_push;
|
|
|
|
|
// import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
// import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
|
// import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
//
|
|
|
|
|
// final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
|
//
|
|
|
|
|
// class AppNotifications {
|
|
|
|
|
// static final AppNotifications _instance = AppNotifications._internal();
|
|
|
|
|
//
|
|
|
|
|
// AppNotifications._internal();
|
|
|
|
|
//
|
|
|
|
|
// factory AppNotifications() => _instance;
|
|
|
|
|
//
|
|
|
|
|
// late HmsApiAvailability hmsApiAvailability;
|
|
|
|
|
//
|
|
|
|
|
// String _huaweiToken = '';
|
|
|
|
|
//
|
|
|
|
|
// Future<void> requestPermissions() async {
|
|
|
|
|
// if (Platform.isIOS) {
|
|
|
|
|
// await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(alert: true, badge: true, sound: true);
|
|
|
|
|
// } else if (Platform.isAndroid) {
|
|
|
|
|
// AndroidFlutterLocalNotificationsPlugin? androidImplementation = flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
|
|
|
|
// bool? granted = await androidImplementation?.requestPermission();
|
|
|
|
|
// if (granted == false) {
|
|
|
|
|
// if (kDebugMode) {
|
|
|
|
|
// print("-------------------- Permission Granted ------------------------");
|
|
|
|
|
// print(granted);
|
|
|
|
|
// }
|
|
|
|
|
// await Permission.notification.request();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void init(String? firebaseToken) async {
|
|
|
|
|
// hmsApiAvailability = HmsApiAvailability();
|
|
|
|
|
// await requestPermissions();
|
|
|
|
|
// AppState().setDeviceToken = firebaseToken;
|
|
|
|
|
// await Permission.notification.isDenied.then((bool value) {
|
|
|
|
|
// if (value) {
|
|
|
|
|
// Permission.notification.request();
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
|
|
|
|
|
//
|
|
|
|
|
// if (initialMessage != null) _handleMessage(initialMessage);
|
|
|
|
|
//
|
|
|
|
|
// FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
|
|
|
// if (message.notification != null) _handleMessage(message);
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// FirebaseMessaging.onMessageOpenedApp.listen(_handleOpenApp);
|
|
|
|
|
//
|
|
|
|
|
// FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
|
|
|
|
//
|
|
|
|
|
// FirebaseMessaging.instance.onTokenRefresh.listen((String token) {
|
|
|
|
|
// AppState().setDeviceToken = token;
|
|
|
|
|
// });
|
|
|
|
|
//
|
|
|
|
|
// if (Platform.isAndroid) {
|
|
|
|
|
// await hmsApiAvailability.isHMSAvailable().then((value) async {
|
|
|
|
|
// if (value == 0) {
|
|
|
|
|
// huawei_push.Push.enableLogger();
|
|
|
|
|
// var result = await huawei_push.Push.setAutoInitEnabled(true);
|
|
|
|
|
//
|
|
|
|
|
// huawei_push.Push.onNotificationOpenedApp.listen((message) {
|
|
|
|
|
// // newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
// }, onError: (e) => print(e.toString()));
|
|
|
|
|
//
|
|
|
|
|
// huawei_push.Push.onMessageReceivedStream.listen((message) {
|
|
|
|
|
// // newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
// }, onError: (e) => print(e.toString()));
|
|
|
|
|
// }
|
|
|
|
|
// }).catchError((err) {
|
|
|
|
|
// print(err);
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void initHuaweiPush(Function loginCallback) {
|
|
|
|
|
// AppState().setIsHuawei = true;
|
|
|
|
|
// initTokenStream(loginCallback);
|
|
|
|
|
// huawei_push.Push.getToken("");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // HUAWEI PUSH TOKEN IMPLEMENTATION
|
|
|
|
|
// void _onTokenEvent(String event) {
|
|
|
|
|
// _huaweiToken = event;
|
|
|
|
|
// AppState().setHuaweiPushToken = _huaweiToken;
|
|
|
|
|
// debugPrint("HUAWEI PUSH TOKEN: $_huaweiToken");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void _onTokenError(Object error) {}
|
|
|
|
|
//
|
|
|
|
|
// Future<void> initTokenStream(Function loginCallback) async {
|
|
|
|
|
// huawei_push.Push.getTokenStream.listen(_onTokenEvent, onError: _onTokenError).onData((data) {
|
|
|
|
|
// AppState().setHuaweiPushToken = data;
|
|
|
|
|
// debugPrint("HUAWEI PUSH TOKEN: $data");
|
|
|
|
|
// loginCallback();
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void _handleMessage(RemoteMessage message) {
|
|
|
|
|
// Utils.saveStringFromPrefs("isAppOpendByChat", "false");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// void _handleOpenApp(RemoteMessage message) {
|
|
|
|
|
// Utils.saveStringFromPrefs("isAppOpendByChat", "true");
|
|
|
|
|
// Utils.saveStringFromPrefs("notificationData", message.data["user_chat_history_response"].toString());
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// AndroidNotificationChannel channel = const AndroidNotificationChannel(
|
|
|
|
|
// "high_importance_channel",
|
|
|
|
|
// "High Importance Notifications",
|
|
|
|
|
// importance: Importance.high,
|
|
|
|
|
// );
|
|
|
|
|
//
|
|
|
|
|
// Future<dynamic> backgroundMessageHandler(RemoteMessage message) async {
|
|
|
|
|
// await Firebase.initializeApp();
|
|
|
|
|
// Utils.saveStringFromPrefs("isAppOpendByChat", "false");
|
|
|
|
|
// Utils.saveStringFromPrefs("notificationData", message.data["user_chat_history_response"].toString());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//************** NEW fILE *****************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
|
import 'package:huawei_hmsavailability/huawei_hmsavailability.dart';
|
|
|
|
|
|
|
|
|
|
// import 'package:huawei_hmsavailability/huawei_hmsavailability.dart';
|
|
|
|
|
import 'package:huawei_push/huawei_push.dart' as huawei_push;
|
|
|
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
|
|
|
import 'package:mohem_flutter_app/classes/utils.dart';
|
|
|
|
|
@ -19,7 +160,7 @@ class AppNotifications {
|
|
|
|
|
|
|
|
|
|
factory AppNotifications() => _instance;
|
|
|
|
|
|
|
|
|
|
late HmsApiAvailability hmsApiAvailability;
|
|
|
|
|
// late HmsApiAvailability hmsApiAvailability;
|
|
|
|
|
|
|
|
|
|
String _huaweiToken = '';
|
|
|
|
|
|
|
|
|
|
@ -40,7 +181,10 @@ class AppNotifications {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init(String? firebaseToken) async {
|
|
|
|
|
hmsApiAvailability = HmsApiAvailability();
|
|
|
|
|
// if (Platform.isAndroid) {
|
|
|
|
|
// hmsApiAvailability = HmsApiAvailability();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
await requestPermissions();
|
|
|
|
|
AppState().setDeviceToken = firebaseToken;
|
|
|
|
|
await Permission.notification.isDenied.then((bool value) {
|
|
|
|
|
@ -65,22 +209,22 @@ class AppNotifications {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
await hmsApiAvailability.isHMSAvailable().then((value) async {
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
huawei_push.Push.enableLogger();
|
|
|
|
|
var result = await huawei_push.Push.setAutoInitEnabled(true);
|
|
|
|
|
|
|
|
|
|
huawei_push.Push.onNotificationOpenedApp.listen((message) {
|
|
|
|
|
// newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
}, onError: (e) => print(e.toString()));
|
|
|
|
|
|
|
|
|
|
huawei_push.Push.onMessageReceivedStream.listen((message) {
|
|
|
|
|
// newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
}, onError: (e) => print(e.toString()));
|
|
|
|
|
}
|
|
|
|
|
}).catchError((err) {
|
|
|
|
|
print(err);
|
|
|
|
|
});
|
|
|
|
|
// await hmsApiAvailability.isHMSAvailable().then((value) async {
|
|
|
|
|
if (!(await Utils.isGoogleServicesAvailable())) {
|
|
|
|
|
huawei_push.Push.enableLogger();
|
|
|
|
|
var result = await huawei_push.Push.setAutoInitEnabled(true);
|
|
|
|
|
|
|
|
|
|
huawei_push.Push.onNotificationOpenedApp.listen((message) {
|
|
|
|
|
// newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
}, onError: (e) => print(e.toString()));
|
|
|
|
|
|
|
|
|
|
huawei_push.Push.onMessageReceivedStream.listen((message) {
|
|
|
|
|
// newMessage(toFirebaseRemoteMessage(message));
|
|
|
|
|
}, onError: (e) => print(e.toString()));
|
|
|
|
|
}
|
|
|
|
|
// }).catchError((err) {
|
|
|
|
|
// print(err);
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -112,8 +256,10 @@ class AppNotifications {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleOpenApp(RemoteMessage message) {
|
|
|
|
|
Utils.saveStringFromPrefs("isAppOpendByChat", "true");
|
|
|
|
|
Utils.saveStringFromPrefs("notificationData", message.data["user_chat_history_response"].toString());
|
|
|
|
|
if (message.data.isNotEmpty && message.data["type"] == 'chat') {
|
|
|
|
|
Utils.saveStringFromPrefs("isAppOpendByChat", "true");
|
|
|
|
|
Utils.saveStringFromPrefs("notificationData", message.data["user_chat_history_response"].toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -127,4 +273,7 @@ Future<dynamic> backgroundMessageHandler(RemoteMessage message) async {
|
|
|
|
|
await Firebase.initializeApp();
|
|
|
|
|
Utils.saveStringFromPrefs("isAppOpendByChat", "false");
|
|
|
|
|
Utils.saveStringFromPrefs("notificationData", message.data["user_chat_history_response"].toString());
|
|
|
|
|
}
|
|
|
|
|
if (message.data.isNotEmpty && message.data["type"] == 'call') {
|
|
|
|
|
// ChatVoipCall().showCallkitIncoming(uuid: const Uuid().v4(), data: message);
|
|
|
|
|
}
|
|
|
|
|
}
|