Merge branch 'master' into development_sikander
commit
84a07df310
@ -0,0 +1,59 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:firebase_messaging/firebase_messaging.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();
|
||||
|
||||
|
||||
class AppNotifications {
|
||||
static final AppNotifications _instance = AppNotifications._internal();
|
||||
|
||||
AppNotifications._internal();
|
||||
|
||||
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();
|
||||
// }
|
||||
// });
|
||||
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);
|
||||
}
|
||||
|
||||
void _handleMessage(RemoteMessage message) {
|
||||
print("Handle Message");
|
||||
logger.w(json.encode(message));
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue