Skip to content

Instantly share code, notes, and snippets.

@deshario
Created April 8, 2020 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deshario/373d4eb35f9ba96c965108bbc785583c to your computer and use it in GitHub Desktop.
Save deshario/373d4eb35f9ba96c965108bbc785583c to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:myapp/model/Message.dart';
class CloudMessaging {
FirebaseMessaging firebaseMessaging;
ValueChanged<Message> onReceived;
CloudMessaging({
@required this.onReceived
});
void initialize() {
firebaseMessaging = FirebaseMessaging();
messageListener();
}
Future<String> getToken(){
return firebaseMessaging.getToken().then((token) {
return token;
});
}
static Future bgMsgHandler(Map<String, dynamic> message) async{
if (message.containsKey('data')) {
final dynamic data = message['data'];
print('Data Message => $data');
}
if (message.containsKey('notification')) {
final dynamic notification = message['notification'];
print('Notification Message => $notification');
}
}
void msgHandler(Map<String, dynamic> message){
final dynamic notification = message['notification'];
String nTitle = notification['title'] != null ? notification['title'] : '';
String nBody = notification['body'] != null ? notification['body'] : '';
String nImage = notification['image'] != null ? notification['image'] : '';
Message msg = new Message(title:nTitle, body:nBody, image:nImage);
onReceived(msg);
}
void messageListener() {
if (Platform.isIOS) iosPermission();
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('onMessage');
msgHandler(message);
},
onBackgroundMessage: Platform.isAndroid ? bgMsgHandler:null,
onResume: (Map<String, dynamic> message) async {
print('onResume');
msgHandler(message);
},
onLaunch: (Map<String, dynamic> message) async {
print('onLaunch');
msgHandler(message);
},
);
}
void iosPermission() {
firebaseMessaging.requestNotificationPermissions(IosNotificationSettings(sound: true, badge: true, alert: true));
firebaseMessaging.onIosSettingsRegistered.listen((IosNotificationSettings settings){
print("FCM onIosSettingsRegistered");
// print("Settings registered: $settings");
});
}
}
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/services.dart';
import 'package:myapp/routes/routes.dart';
import 'package:myapp/routes/application.dart';
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
MyAppState() {
final router = Router();
Routes.configureRoutes(router);
Application.router = router;
}
@override
void initState() {
super.initState();
}
Widget build(BuildContext context) {
final app = MaterialApp(
title: 'myapp',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute : Routes.root,
onGenerateRoute: Application.router.generator,
);
return app;
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
runApp(new MyApp());
});
}
import 'package:flutter/material.dart';
class Message{
String title,body,image;
Message({
@required this.title,
@required this.body,
this.image,
});
String getTitle() => this.title;
void setTitle(title) => this.title = title;
String getBody() => this.body;
void setBody(body) => this.body = body;
String getImage() => this.image;
void setImage(image) => this.image = image;
}
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:myapp/config/Messaging.dart';
import 'package:myapp/model/Message.dart';
import 'package:myapp/screens/auth/main.dart';
import 'package:myapp/screens/index.dart';
import 'package:myapp/service/auth.dart';
import 'package:myapp/service/index.dart';
import 'package:myapp/components/Loader.dart';
import 'package:myapp/config/Store.dart';
class LandingPage extends StatefulWidget {
@override
LandingPageState createState() => LandingPageState();
}
class LandingPageState extends State<LandingPage> {
StreamBuilder<FirebaseUser> builder;
final FBService fbService = new FBService();
final AuthService auth = AuthService();
CloudMessaging messaging = new CloudMessaging(
onReceived: (Message msg){
print(msg.getTitle());
}
);
Store store = new Store();
@override
void initState() {
super.initState();
messaging.initialize();
}
Future<String> getRole(uid) async {
Map checkRole = await fbService.checkUser(uid);
return checkRole['type'];
}
void showMessageDialog(){ // Show Dialog onMessage Received
// showDialog(
// context: context,
// barrierDismissible: false,
// builder: (BuildContext context) => buildDialog(context)
// );
}
@override
Widget build(BuildContext context) {
builder = new StreamBuilder(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
FirebaseUser user = snapshot.data;
if (user == null) {
return MainLogin();
}
messaging.getToken().then((token) {
print('Token => $token');
auth.setToken(user.uid, token);
store.updateToken(token);
});
return FutureBuilder(
future: getRole(user.uid),
builder: (BuildContext context, AsyncSnapshot mSnapshot) {
if (mSnapshot.connectionState == ConnectionState.done) {
String role = mSnapshot.data;
if (role == 'admin') {
return AdminIndex(uID: user.uid);
} else if (role == 'shop') {
return ShopIndex(uID: user.uid);
} else if (role == 'hub') {
return HubIndex(uID: user.uid);
} else if (role == 'rider') {
return RiderIndex(uID: user.uid);
} else {
return Loader();
}
} else {
return Loader();
}
},
);
} else {
return Loader();
}
}
);
return Scaffold(
body: builder,
);
}
@override
void dispose() {
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment