Notification

development_mirza
Aamir Muhammad 2 years ago
parent 81a060012e
commit 698aee3482

@ -50,6 +50,7 @@
<string>This app requires photo library access to select image as document &amp; upload it.</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
<key>FirebaseAppDelegateProxyEnabled</key>

@ -1,12 +1,13 @@
import 'dart:convert';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/main.dart';
import 'package:permission_handler/permission_handler.dart';
//final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
class AppNotifications {
static final AppNotifications _instance = AppNotifications._internal();
@ -15,45 +16,63 @@ class AppNotifications {
factory AppNotifications() => _instance;
// 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) {
// print("-------------------- Permission Granted ------------------------");
// print(granted);
// await Permission.notification.request();
// }
// }
// }
// Future<void> isAndroidPermGranted() async {
// if (Platform.isAndroid) {
// bool granted = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.areNotificationsEnabled() ?? false;
// }
// }
void initNotification(String? firebaseToken) async {
// await requestPermissions();
AppState().deviceNotificationToken = firebaseToken;
// await Permission.notification.isDenied.then((value) {
// if (value) {
// Permission.notification.request();
// }
// });
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 {
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(_handleMessage);
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
FirebaseMessaging.instance.onTokenRefresh.listen((String token) {
AppState().setDeviceToken = token;
});
}
void _handleMessage(RemoteMessage message) {
print("Handle Message");
if (kDebugMode) {
print("Handle Message");
}
logger.w(json.encode(message));
}
}
AndroidNotificationChannel channel = const AndroidNotificationChannel(
"high_importance_channel",
"High Importance Notifications",
importance: Importance.high,
);
Future<dynamic> backgroundMessageHandler(RemoteMessage message) async {
if (kDebugMode) {
print("Firebase backgroundMessageHandler!!!");
}
}

@ -1,59 +0,0 @@
import 'dart:async';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
// |--> Push Notification Background
Future<dynamic> backgroundMessageHandler(message) async {
print("Firebase backgroundMessageHandler!!!");
}
class PushNotificationHandler {
final BuildContext context;
static PushNotificationHandler? _instance;
PushNotificationHandler(this.context) {
PushNotificationHandler._instance = this;
}
static PushNotificationHandler getInstance() => _instance!;
void init() async {
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
if (Platform.isIOS) {
await Future.delayed(Duration(milliseconds: 3000)).then((value) {
newMessage(message);
});
} else {
newMessage(message);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
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);
AppState().setDeviceToken = fcm_token;
});
FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
}
void newMessage(RemoteMessage remoteMessage) async {
print("Remote Message: " + remoteMessage.data.toString());
if (remoteMessage.data.isEmpty) {
return;
}
}
}

@ -59,7 +59,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
marathonProvider = Provider.of<MarathonProvider>(context, listen: false);
cProvider = Provider.of<ChatProviderModel>(context, listen: false);
_bHubCon();
_onRefresh();
_onRefresh(true);
});
}
@ -78,7 +78,17 @@ class _DashboardScreenState extends State<DashboardScreen> {
});
}
void _onRefresh() async {
Future<void> checkHubCon() async {
print("-------------------------Again Hub Connection --------------------------------");
if (chatHubConnection.state == HubConnectionState.Connected) {
await chatHubConnection.stop();
await chatHubConnection.start();
} else if (chatHubConnection.state != HubConnectionState.Connected) {
await chatHubConnection.start();
}
}
void _onRefresh(bool isFromInit) async {
data.initProvider();
// data.getITGNotification().then((value) {
// print("--------------------detail_1-----------------");
@ -93,6 +103,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
data.fetchMenuEntries();
data.getCategoryOffersListAPI(context);
marathonProvider.getMarathonDetailsFromApi();
if (!isFromInit) checkHubCon();
_refreshController.refreshCompleted();
}
@ -192,7 +203,9 @@ class _DashboardScreenState extends State<DashboardScreen> {
color: MyColors.gradiantEndColor,
),
controller: _refreshController,
onRefresh: _onRefresh,
onRefresh: () {
_onRefresh(false);
},
child: SingleChildScrollView(
child: Column(
children: [

@ -13,7 +13,6 @@ import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/classes/notifications.dart';
import 'package:mohem_flutter_app/classes/push-notification-handler.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
@ -97,7 +96,7 @@ class _LoginScreenState extends State<LoginScreen> {
await Firebase.initializeApp();
_firebaseMessaging = FirebaseMessaging.instance;
firebaseToken = await _firebaseMessaging.getToken();
AppNotifications().initNotification(firebaseToken);
AppNotifications().init(firebaseToken);
loginInfo = await LoginApiClient().getMobileLoginInfoNEW(firebaseToken ?? "", Platform.isAndroid ? "android" : "ios");
if (loginInfo == null) {
await checkPrefs();

@ -92,7 +92,7 @@ dependencies:
swipe_to: ^1.0.2
flutter_webrtc: ^0.9.16
camera: ^0.10.0+4
#flutter_local_notifications: any
flutter_local_notifications: any
#Chat Voice Message Recoding & Play
audio_waveforms: ^0.1.5+1

Loading…
Cancel
Save