add messaging
This commit is contained in:
75
lib/app.dart
75
lib/app.dart
@@ -1,6 +1,9 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:fcs/fcs/common/localization/app_translations_delegate.dart';
|
||||
import 'package:fcs/fcs/common/localization/transalation.dart';
|
||||
import 'package:fcs/fcs/common/pages/model/language_model.dart';
|
||||
import 'package:fcs/fcs/common/services/services.dart';
|
||||
import 'package:fcs/model/buyer_model.dart';
|
||||
import 'package:fcs/model/delivery_model.dart';
|
||||
import 'package:fcs/model/discount_model.dart';
|
||||
@@ -17,6 +20,7 @@ import 'package:fcs/model_fcs/package_model.dart';
|
||||
import 'package:fcs/pages/email_page.dart';
|
||||
import 'package:fcs/pages/login_page.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:fcs/fcs/common/pages/model/main_model.dart' as fcs;
|
||||
@@ -82,6 +86,8 @@ class _AppState extends State<App> {
|
||||
final DiscountModel discountModel = new DiscountModel();
|
||||
|
||||
AppTranslationsDelegate _newLocaleDelegate;
|
||||
static FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -121,6 +127,64 @@ class _AppState extends State<App> {
|
||||
..addModel(customerModel)
|
||||
..addModel(discountModel);
|
||||
this.mainModel.init();
|
||||
|
||||
_initLocalNotifications();
|
||||
Services.instance.messagingService.init((message) {
|
||||
print("Message from FCM:$message");
|
||||
_showNotification(message);
|
||||
});
|
||||
}
|
||||
|
||||
_initLocalNotifications() {
|
||||
var initializationSettingsAndroid =
|
||||
new AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
var initializationSettingsIOS = new IOSInitializationSettings();
|
||||
var initializationSettings = new InitializationSettings(
|
||||
initializationSettingsAndroid, initializationSettingsIOS);
|
||||
_flutterLocalNotificationsPlugin.initialize(initializationSettings);
|
||||
}
|
||||
|
||||
static Future _showNotification(Map<String, dynamic> message) async {
|
||||
var pushTitle;
|
||||
var pushText;
|
||||
var action;
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
var nodeData = message['notification'];
|
||||
pushTitle = nodeData['title'];
|
||||
pushText = nodeData['body'];
|
||||
action = nodeData['action'];
|
||||
} else {
|
||||
pushTitle = message['title'];
|
||||
pushText = message['body'];
|
||||
action = message['action'];
|
||||
}
|
||||
print("AppPushs params pushTitle : $pushTitle");
|
||||
print("AppPushs params pushText : $pushText");
|
||||
print("AppPushs params pushAction : $action");
|
||||
|
||||
// @formatter:off
|
||||
var platformChannelSpecificsAndroid = new AndroidNotificationDetails(
|
||||
'your channel id', 'your channel name', 'your channel description',
|
||||
playSound: true,
|
||||
enableVibration: true,
|
||||
importance: Importance.Max,
|
||||
priority: Priority.High);
|
||||
// @formatter:on
|
||||
var platformChannelSpecificsIos =
|
||||
new IOSNotificationDetails(presentSound: true);
|
||||
var platformChannelSpecifics = new NotificationDetails(
|
||||
platformChannelSpecificsAndroid, platformChannelSpecificsIos);
|
||||
|
||||
new Future.delayed(Duration.zero, () {
|
||||
_flutterLocalNotificationsPlugin.show(
|
||||
0,
|
||||
pushTitle,
|
||||
pushText,
|
||||
platformChannelSpecifics,
|
||||
payload: 'No_Sound',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void onLocaleChange(Locale locale) {
|
||||
@@ -178,11 +242,9 @@ class _AppState extends State<App> {
|
||||
ChangeNotifierProvider.value(value: testModel),
|
||||
ChangeNotifierProvider.value(value: mainModel2),
|
||||
],
|
||||
child: Consumer<LanguageModel>(
|
||||
builder: (BuildContext context, LanguageModel value, Widget child) {
|
||||
return MaterialApp(
|
||||
child: MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Ok Energy',
|
||||
title: 'FCS',
|
||||
routes: route(context),
|
||||
theme: ThemeData(accentColor: Colors.black),
|
||||
localizationsDelegates: [
|
||||
@@ -192,9 +254,6 @@ class _AppState extends State<App> {
|
||||
//provides RTL support
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: Translation().supportedLocales());
|
||||
},
|
||||
),
|
||||
);
|
||||
supportedLocales: Translation().supportedLocales()));
|
||||
}
|
||||
}
|
||||
|
||||
66
lib/fcs/common/data/providers/messaging_fcm.dart
Normal file
66
lib/fcs/common/data/providers/messaging_fcm.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'package:fcs/fcs/common/services/messaging_service.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
final msgLog = Logger('backgroundMessageHandler');
|
||||
|
||||
Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {
|
||||
if (message.containsKey('data')) {
|
||||
// Handle data message
|
||||
final dynamic data = message['data'];
|
||||
msgLog.info("background onMessage: $message");
|
||||
}
|
||||
|
||||
if (message.containsKey('notification')) {
|
||||
// Handle notification message
|
||||
final dynamic notification = message['notification'];
|
||||
}
|
||||
}
|
||||
|
||||
class MessagingFCM {
|
||||
final log = Logger('MessagingFCM');
|
||||
|
||||
FirebaseMessaging _firebaseMessaging;
|
||||
|
||||
MessagingFCM(OnNotify onMessage, {OnNotify onLaunch, OnNotify onResume}) {
|
||||
_firebaseMessaging = FirebaseMessaging();
|
||||
_firebaseMessaging.configure(
|
||||
onMessage: (Map<String, dynamic> message) async {
|
||||
log.info("onMessage: $message");
|
||||
if (onMessage != null) _onNotify(message, onMessage);
|
||||
},
|
||||
onBackgroundMessage: backgroundMessageHandler,
|
||||
onLaunch: (Map<String, dynamic> message) async {
|
||||
log.info("onLaunch: $message");
|
||||
if (onLaunch != null) _onNotify(message, onLaunch);
|
||||
},
|
||||
onResume: (Map<String, dynamic> message) async {
|
||||
log.info("onResume: $message");
|
||||
if (onResume != null) _onNotify(message, onResume);
|
||||
},
|
||||
);
|
||||
_firebaseMessaging.requestNotificationPermissions(
|
||||
const IosNotificationSettings(
|
||||
sound: true, badge: true, alert: true, provisional: true));
|
||||
_firebaseMessaging.onIosSettingsRegistered
|
||||
.listen((IosNotificationSettings settings) {
|
||||
log.info("Settings registered: $settings");
|
||||
});
|
||||
_firebaseMessaging.getToken().then((String token) {
|
||||
log.info("Messaging Token:$token");
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> subscribeToTopic(String topic) {
|
||||
return _firebaseMessaging.subscribeToTopic(topic);
|
||||
}
|
||||
|
||||
_onNotify(Map<String, dynamic> message, OnNotify onNotify) {
|
||||
var data = message['data'] ?? message;
|
||||
onNotify(Map<String, dynamic>.from(message));
|
||||
}
|
||||
|
||||
Future<void> unsubscribeToTopic(String topic) {
|
||||
return _firebaseMessaging.unsubscribeFromTopic(topic);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import 'package:fcs/fcs/common/domain/entities/auth_status.dart';
|
||||
import 'package:fcs/fcs/common/domain/entities/user.dart';
|
||||
import 'package:fcs/fcs/common/pages/model/main_model.dart';
|
||||
import 'package:fcs/fcs/common/pages/util.dart';
|
||||
import 'package:fcs/fcs/common/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/widget/bottom_up_page_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pin_input_text_field/pin_input_text_field.dart';
|
||||
@@ -12,7 +13,6 @@ import 'package:provider/provider.dart';
|
||||
|
||||
import 'signup_page.dart';
|
||||
import '../../helpers/theme.dart';
|
||||
import '../../../../widget/local_text.dart';
|
||||
import '../../../../widget/progress.dart';
|
||||
|
||||
const resend_count_sec = 5;
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:country_code_picker/country_code_picker.dart';
|
||||
import 'package:fcs/fcs/common/domain/entities/auth_result.dart';
|
||||
import 'package:fcs/fcs/common/domain/entities/auth_status.dart';
|
||||
import 'package:fcs/fcs/common/pages/model/main_model.dart';
|
||||
import 'package:fcs/fcs/common/pages/widgets/local_text.dart';
|
||||
import 'package:fcs/widget/bottom_up_page_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -9,7 +10,6 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../helpers/theme.dart';
|
||||
import '../../../../widget/local_text.dart';
|
||||
import '../../../../widget/progress.dart';
|
||||
import 'code_page.dart';
|
||||
import '../util.dart';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import 'package:fcs/fcs/common/pages/model/language_model.dart';
|
||||
import 'package:fcs/fcs/common/pages/model/main_model.dart';
|
||||
import 'package:fcs/fcs/common/pages/widgets/bottom_widgets.dart';
|
||||
import 'package:fcs/model/language_model.dart';
|
||||
import 'package:fcs/widget/banner.dart';
|
||||
import 'package:fcs/widget/bottom_up_page_route.dart';
|
||||
import 'package:fcs/widget/localization/transalation.dart';
|
||||
@@ -194,7 +194,7 @@ class _WelcomePageState extends State<WelcomePage> {
|
||||
}
|
||||
|
||||
_langChange(index) {
|
||||
var languageModel = Provider.of<LanguageModel>(context);
|
||||
var languageModel = Provider.of<LanguageModel>(context, listen: false);
|
||||
languageModel.saveLanguage(Translation().supportedLanguages[index]);
|
||||
setState(() {
|
||||
isSelected.asMap().forEach((i, e) {
|
||||
|
||||
@@ -23,8 +23,8 @@ class ActionButton extends StatelessWidget {
|
||||
onTap: btnCallback != null ? btnCallback : () => {},
|
||||
child: Container(
|
||||
width: 120,
|
||||
height: 150,
|
||||
padding: EdgeInsets.only(top:15.0),
|
||||
height: 130,
|
||||
padding: EdgeInsets.only(top: 10.0, left: 5, right: 5),
|
||||
decoration: new BoxDecoration(
|
||||
color: Colors.transparent,
|
||||
borderRadius: new BorderRadius.only(
|
||||
|
||||
25
lib/fcs/common/services/messaging_imp.dart
Normal file
25
lib/fcs/common/services/messaging_imp.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:fcs/fcs/common/data/providers/messaging_fcm.dart';
|
||||
|
||||
import 'messaging_service.dart';
|
||||
|
||||
class MessagingServiceImp implements MessagingService {
|
||||
MessagingServiceImp();
|
||||
|
||||
static MessagingFCM messagingFCM;
|
||||
|
||||
@override
|
||||
void init(onMessage, {OnNotify onLaunch, OnNotify onResume}) {
|
||||
messagingFCM =
|
||||
MessagingFCM(onMessage, onLaunch: onLaunch, onResume: onResume);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> subscribe(String topic) {
|
||||
return messagingFCM.unsubscribeToTopic(topic);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> unsubscribe(String topic) {
|
||||
return messagingFCM.unsubscribeToTopic(topic);
|
||||
}
|
||||
}
|
||||
7
lib/fcs/common/services/messaging_service.dart
Normal file
7
lib/fcs/common/services/messaging_service.dart
Normal file
@@ -0,0 +1,7 @@
|
||||
typedef OnNotify(Map<String, dynamic> message);
|
||||
|
||||
abstract class MessagingService {
|
||||
void init(OnNotify onMessage, {OnNotify onLaunch, OnNotify onResume});
|
||||
Future<void> subscribe(String topic);
|
||||
Future<void> unsubscribe(String topic);
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import 'package:fcs/fcs/common/data/providers/auth_fb.dart';
|
||||
import 'package:fcs/fcs/common/services/auth_imp.dart';
|
||||
import 'package:fcs/fcs/common/services/messaging_imp.dart';
|
||||
import 'package:fcs/fcs/common/services/messaging_service.dart';
|
||||
|
||||
import 'auth_service.dart';
|
||||
|
||||
@@ -7,13 +9,16 @@ class Services {
|
||||
static final Services instance = Services._();
|
||||
|
||||
AuthService _authService;
|
||||
MessagingService _messagingService;
|
||||
Services._() {
|
||||
_authService = AuthServiceImp(
|
||||
authFb: AuthFb.instance,
|
||||
connectivity: null,
|
||||
userFBDataProvider: null,
|
||||
userLocalDataProvider: null);
|
||||
_messagingService = MessagingServiceImp();
|
||||
}
|
||||
|
||||
AuthService get authService => _authService;
|
||||
MessagingService get messagingService => _messagingService;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:logging/logging.dart';
|
||||
import 'app.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Config(
|
||||
flavor: Flavor.DEV,
|
||||
color: Colors.blue,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import 'package:fcs/config.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:fcs/config.dart';
|
||||
|
||||
import 'app.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Config(
|
||||
flavor: Flavor.DEV,
|
||||
color: Colors.blue,
|
||||
apiURL: "http://192.168.1.155:7777",
|
||||
apiURL: "http://192.168.100.11:7777",
|
||||
level: Level.ALL);
|
||||
runApp(App());
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'app.dart';
|
||||
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
Config(
|
||||
flavor: Flavor.PRODUCTION,
|
||||
color: Colors.blue,
|
||||
|
||||
@@ -73,6 +73,7 @@ dependencies:
|
||||
timeline_list: ^0.0.5
|
||||
barcode_scan: ^3.0.1
|
||||
flutter_pdfview: ^1.0.3
|
||||
flutter_local_notifications: ^1.4.4+4
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user