|
|
|
|
@ -13,6 +13,7 @@ import 'package:diplomaticquarterapp/pages/webRTC/OpenTok/OpenTok.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/LocalNotification.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/app-permissions.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
|
|
|
|
|
import 'package:diplomaticquarterapp/uitl/utils.dart';
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart' as fir;
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
|
@ -163,7 +164,6 @@ class PushNotificationHandler {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init() async {
|
|
|
|
|
hmsApiAvailability = new HmsApiAvailability();
|
|
|
|
|
// VoIP Callbacks
|
|
|
|
|
voIPKit.getVoIPToken().then((value) {
|
|
|
|
|
print('🎈 example: getVoIPToken: $value');
|
|
|
|
|
@ -226,83 +226,7 @@ class PushNotificationHandler {
|
|
|
|
|
// if (Platform.isAndroid && (!await FlutterHmsGmsAvailability.isHmsAvailable)) {
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
try {
|
|
|
|
|
await hmsApiAvailability.isHMSAvailable().then((value) async {
|
|
|
|
|
if (value != 0) {
|
|
|
|
|
final fcmToken = await FirebaseMessaging.instance.getToken();
|
|
|
|
|
if (fcmToken != null) onToken(fcmToken);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
|
final permission = await FirebaseMessaging.instance.requestPermission();
|
|
|
|
|
if (permission.authorizationStatus == AuthorizationStatus.denied) return;
|
|
|
|
|
} else {
|
|
|
|
|
// await Permission.notification.request().then((value) {
|
|
|
|
|
// }).catchError((err) {
|
|
|
|
|
// print(err);
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 'Android HMS' (Handle Huawei Push_Kit Streams)
|
|
|
|
|
// if (Platform.isAndroid) {
|
|
|
|
|
// if (Platform.isAndroid && (await FlutterHmsGmsAvailability.isHmsAvailable)) {
|
|
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
// 'Android GMS or iOS' (Handle Firebase Messaging Streams
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) async {
|
|
|
|
|
if (message != null) {
|
|
|
|
|
// print("Firebase getInitialMessage with message : ${message.data.toString()}");
|
|
|
|
|
subscribeFCMTopic();
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
if (message != null) newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else if (message != null) newMessage(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
|
|
|
|
|
print("Firebase onMessage!!!");
|
|
|
|
|
// Utils.showPermissionConsentDialog(context, "onMessage", (){});
|
|
|
|
|
// newMessage(message);
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
|
|
|
|
|
print("Firebase onMessageOpenedApp!!!");
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.instance.onTokenRefresh.listen((fcm_token) {
|
|
|
|
|
print("Push Notification onTokenRefresh: " + fcm_token);
|
|
|
|
|
onToken(fcm_token);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.instance.getToken(vapidKey: 'BHRJG8sIzcysWxPw3B6xQjz_85nUuCfU6EAmpH18kyUTmB2cj35IdFwCyWSab80SA1v6oBSWVh-p6PcHPw_y00Y').then((String token) {
|
|
|
|
|
print("Push Notification getToken: " + token);
|
|
|
|
|
onToken(token);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
|
|
|
|
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
await hmsApiAvailability.isHMSAvailable().then((value) async {
|
|
|
|
|
if (value == 0) {
|
|
|
|
|
if (!(await Utils.isGoogleServicesAvailable())) {
|
|
|
|
|
h_push.Push.enableLogger();
|
|
|
|
|
final result = await h_push.Push.setAutoInitEnabled(true);
|
|
|
|
|
|
|
|
|
|
@ -320,18 +244,61 @@ class PushNotificationHandler {
|
|
|
|
|
await h_push.Push.getToken('');
|
|
|
|
|
|
|
|
|
|
h_push.Push.registerBackgroundMessageHandler(backgroundMessageHandler);
|
|
|
|
|
} else {
|
|
|
|
|
final fcmToken = await FirebaseMessaging.instance.getToken();
|
|
|
|
|
if (fcmToken != null) onToken(fcmToken);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) async {
|
|
|
|
|
if (message != null) {
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
if (message != null) newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else if (message != null) newMessage(message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
|
|
|
|
|
print("Firebase onMessage!!!");
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
|
|
|
|
|
print("Firebase onMessageOpenedApp!!!");
|
|
|
|
|
if (Platform.isIOS)
|
|
|
|
|
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
else
|
|
|
|
|
newMessage(message);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.instance.onTokenRefresh.listen((fcm_token) {
|
|
|
|
|
print("Push Notification onTokenRefresh: " + fcm_token);
|
|
|
|
|
onToken(fcm_token);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.instance.getToken(vapidKey: 'BHRJG8sIzcysWxPw3B6xQjz_85nUuCfU6EAmpH18kyUTmB2cj35IdFwCyWSab80SA1v6oBSWVh-p6PcHPw_y00Y').then((String token) {
|
|
|
|
|
print("Push Notification getToken: " + token);
|
|
|
|
|
onToken(token);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
|
|
|
|
|
}
|
|
|
|
|
}).catchError((err) {
|
|
|
|
|
print(err);
|
|
|
|
|
});
|
|
|
|
|
} catch (ex) {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscribeFCMTopic() async {
|
|
|
|
|
print("subscribeFCMTopic!!!");
|
|
|
|
|
await FirebaseMessaging.instance.unsubscribeFromTopic('all_hmg_patients').then((value) async {
|
|
|
|
|
await FirebaseMessaging.instance.subscribeToTopic('all_hmg_patients');
|
|
|
|
|
});
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
|
final permission = await FirebaseMessaging.instance.requestPermission();
|
|
|
|
|
if (permission.authorizationStatus == AuthorizationStatus.denied) return;
|
|
|
|
|
} else {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newMessage(RemoteMessage remoteMessage) async {
|
|
|
|
|
@ -374,10 +341,7 @@ class PushNotificationHandler {
|
|
|
|
|
Future<void> isAndroidPermissionGranted() async {
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
final bool granted = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.areNotificationsEnabled() ?? false;
|
|
|
|
|
if (granted) {
|
|
|
|
|
print("-------------------- Permission Granted ------------------------");
|
|
|
|
|
print(granted);
|
|
|
|
|
} else {
|
|
|
|
|
if (!granted) {
|
|
|
|
|
await requestPermissions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|