Skip to content

Instantly share code, notes, and snippets.

@beatscode
Created October 16, 2018 15:31
Show Gist options
  • Save beatscode/7a1ff933b0a05a6763aaad96266dd648 to your computer and use it in GitHub Desktop.
Save beatscode/7a1ff933b0a05a6763aaad96266dd648 to your computer and use it in GitHub Desktop.
Flutter Push Notification - Navigation Failure
import 'package:flutter/material.dart';
import 'package:MyTesttv/backend/users.context.dart';
import 'dart:async';
import './ui/contactusPage.dart';
import './ui/calendarPage.dart';
import './ui/videosPage.dart';
import './ui/faqPage.dart';
import './ui/interestsPage.dart';
import './ui/profilePage.dart';
import './ui/alternate_sign_up_with_country_codePage.dart';
import './ui/sign_upPage.dart';
import './ui/splashPage.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
final GlobalKey<NavigatorState> navigatorKey =
GlobalKey(debugLabel: "Main Navigator");
void main() {
runApp(new MaterialApp(
navigatorKey: navigatorKey,
home: MyTestApp(),
title: 'MyTesttv',
// Start the app with the "/" named route. In our case, the app will start
// on the FirstScreen Widget
// initialRoute: '/',
routes: {
// When we navigate to the "/" route, build the FirstScreen Widget
// '/': (context) => splashPage(),
'/contactus': (context) => contactusPage(),
'/calendar': (context) => calendarPage(),
'/video': (context) => videosPage(),
'/faq': (context) => faqPage(),
'/interests': (context) => interestsPage(),
'/profile': (context) => profilePage(),
'/alternate_sign_up_with_country_code': (context) =>
alternate_sign_up_with_country_codePage(),
'/sign_up': (context) => sign_upPage(),
'/splash': (context) => splashPage(),
},
));
}
class MyTestApp extends StatefulWidget {
MyTestApp({Key key}) : super(key: key);
@override
MyTestAppState createState() => new MyTestAppState();
}
class MyTestAppState extends State<MyTestApp> {
// final GlobalKey<NavigatorState> navigatorKey = new GlobalKey();
static const loading = const Duration(seconds: 3);
final FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
@override
void initState() {
// TODO: implement initState
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage:1 $message");
// Navigator.push(navigatorKey.currentContext,
// MaterialPageRoute(builder: (_) => new videosPage()));
// Navigator.push(context, MaterialPageRoute(builder: (context) => new videosPage()));
// if (Navigator.canPop(context)) {
// Navigator.popUntil(
// context, (Route<dynamic> route) => route is PageRoute);
// }
//
// Navigator.of(context).pushReplacementNamed('/video');
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
//Navigator.of(navigatorKey.currentContext)
// .pushReplacementNamed('/video');
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
// Navigator.of(navigatorKey.currentContext)
// .pushReplacementNamed('/video');
},
);
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
print(token);
});
//startLoading(3);
}
Future<Timer> startLoading(int seconds) async {
var duration = seconds == null ? loading : new Duration(seconds: seconds);
return new Timer(duration, doesUserFileExist);
}
void doesUserFileExist() {
Users.doesUserFileExist().then((exists) {
if (exists == true) {
Navigator.of(context).pushReplacementNamed('/video');
} else {
// Navigator.pushReplacementNamed()
Navigator.of(context).pushReplacementNamed('/sign_up');
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
//key: navigatorKey,
body: new GestureDetector(
onTap: () {
print("Clicked");
},
child: Stack(
fit: StackFit.expand,
children: <Widget>[
new Positioned(
child: new Image.asset("images/SplashBackground.png",
fit: BoxFit.fill)),
new Container(
child: new Image.asset("images/MyTestLogo.png"),
alignment: Alignment(0.0, 0.0),
)
],
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment