-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.dart
More file actions
192 lines (179 loc) · 6.97 KB
/
main.dart
File metadata and controls
192 lines (179 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'package:codephile/homescreen.dart';
import 'package:codephile/resources/colors.dart';
import 'package:codephile/screens/login/login_screen.dart';
import 'package:intent/intent.dart' as intent;
import 'package:intent/action.dart' as action;
import 'package:intent/extra.dart' as extra;
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:codephile/screens/on_boarding/on_boarding_screen.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/subjects.dart';
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
BehaviorSubject<ReceivedNotification>();
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
NotificationAppLaunchDetails notificationAppLaunchDetails;
class ReceivedNotification {
final int id;
final String title;
final String body;
final String payload;
ReceivedNotification(
{@required this.id,
@required this.title,
@required this.body,
@required this.payload});
}
Future<void> main() async {
// needed if you intend to initialize in the `main` function
WidgetsFlutterBinding.ensureInitialized();
notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
var initializationSettingsAndroid = AndroidInitializationSettings('logo');
// Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
// of the `IOSFlutterLocalNotificationsPlugin` class
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification:
(int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject.add(ReceivedNotification(
id: id, title: title, body: body, payload: payload));
});
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
selectNotificationSubject.add(payload);
});
Crashlytics.instance.enableInDevMode = true;
FlutterError.onError = Crashlytics.instance.recordFlutterError;
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print(message.toString());
},
onLaunch: (Map<String, dynamic> message) async {
print(message.toString());
},
onResume: (Map<String, dynamic> message) async {
print(message.toString());
},
);
runApp(
MaterialApp(
home: MyApp(),
debugShowCheckedModeBanner: false,
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new ChooseHome();
}
}
class ChooseHome extends StatefulWidget {
@override
ChooseHomeState createState() => new ChooseHomeState();
}
class ChooseHomeState extends State<ChooseHome> {
Future checkFirstSeen() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = (prefs.getBool('seen') ?? false);
final RemoteConfig _remoteConfig = await RemoteConfig.instance;
final int version = 4;
final defaults = <String, int>{'version': version};
await _remoteConfig.setDefaults(defaults);
await _remoteConfig.fetch(expiration: Duration(seconds: 5));
await _remoteConfig.activateFetched();
final int minimunVersion = _remoteConfig.getInt('version');
print('Minimum version:- ' + minimunVersion.toString());
if (version < minimunVersion) {
print("1");
showDialog(
context: context,
barrierDismissible: false,
builder: (context) => AlertDialog(
titlePadding: EdgeInsets.all(0),
title: Container(
padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
decoration: BoxDecoration(
color: Color(0xFFF3F4F7),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15))),
child: Text(
"Update Available",
textAlign: TextAlign.center,
),
),
contentPadding: EdgeInsets.all(0),
content: Padding(
padding: EdgeInsets.fromLTRB(30, 30, 30, 30),
child: Text(
"This version of the application has been depricated, please update your app through the Google PlayStore.",
textAlign: TextAlign.center,
),
),
actions: <Widget>[
FlatButton(
padding: EdgeInsets.all(15),
onPressed: () {
intent.Intent()
..setAction(action.Action.ACTION_SHOW_APP_INFO)
..putExtra(extra.Extra.EXTRA_PACKAGE_NAME,
"in.ac.iitr.mdg.codephile")
..startActivity().catchError((e) => print(e));
},
child: Container(
padding: EdgeInsets.fromLTRB(40, 10, 40, 10),
color: codephileMain,
child:
Text("Okay", style: TextStyle(color: Colors.white)),
))
],
actionsPadding: EdgeInsets.all(0),
));
} else {
if (_seen) {
String token = prefs.getString('token');
String uid = prefs.getString('uid');
if (token != null && uid != null) {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (context) => HomePage(token: token, userId: uid)));
} else {
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => LoginScreen()));
}
} else {
prefs.setBool('seen', true);
Navigator.of(context).pushReplacement(
new MaterialPageRoute(builder: (context) => OnBoardingScreen()));
}
}
}
@override
void initState() {
super.initState();
checkFirstSeen();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text(""),
),
);
}
}