-
Notifications
You must be signed in to change notification settings - Fork 14
Description
Here is the code. In this code at function onselectnotification while ontapping notification to navigate screen is not working in conditional part (else).Kindly solve this ASAP.
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:settleme/Screens/Dashboard/Settings/SettingsScreen.dart';
import 'package:settleme/Screens/Login/LoginScreen.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
AndroidNotificationChannel? channel;
FlutterLocalNotificationsPlugin? flutterLocalNotificationsPlugin;
late FirebaseMessaging messaging;
final GlobalKey navigatorKey = new GlobalKey();
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
messaging = FirebaseMessaging.instance;
messaging.subscribeToTopic("TopicToListen");
// Set the background messaging handler early on, as a named top-level function
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
if (!kIsWeb) {
channel = const AndroidNotificationChannel(
'flutter_notification_title', // title
'flutter_notification_description', // description
importance: Importance.high,
enableLights: true,
enableVibration: true,
showBadge: true,
playSound: true);
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin!
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel!);
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
navigatorKey: navigatorKey,
title: 'Flutter Notification',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
final String? message;
HomePage({Key? key, this.message}) : super(key: key);
@OverRide
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
bool _isLoggedIn = false;
@OverRide
void initState() {
super.initState();
final android =
AndroidInitializationSettings('@drawable/ic_notification');
final iOS = IOSInitializationSettings();
final initSettings = InitializationSettings(android: android, iOS: iOS);
flutterLocalNotificationsPlugin
?.initialize(initSettings, onSelectNotification: onSelectNotification);
setupInteractedMessage();
FirebaseMessaging.onMessage.listen((message) async {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null && !kIsWeb) {
String action = jsonEncode(message.data);
flutterLocalNotificationsPlugin!.show(
notification.hashCode,
notification.title,
notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel!.name,
channel!.description.toString(),
priority: Priority.high,
importance: Importance.max,
setAsGroupSummary: true,
styleInformation: DefaultStyleInformation(true, true),
largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),
channelShowBadge: true,
autoCancel: true,
icon: '@drawable/ic_notification',
),
),
payload: action);
}
});
FirebaseMessaging.onMessageOpenedApp
.listen((message) => _handleMessage(message.data));
}
void onSelectNotification(String? payload) async {
Future _prefs = SharedPreferences.getInstance();
final SharedPreferences prefs = await _prefs;
_isLoggedIn = prefs.getBool('_isLoggedIn')??false;
setState(() {
if (_isLoggedIn == true)
{
// isLoading = true;
navigatorKey.currentState!.push(MaterialPageRoute(builder: (context) => SettingsScreen()));
}
else{
navigatorKey.currentState!.push(MaterialPageRoute(builder: (context) => Loginpage()));
}
});
// if(payload!.length<6) {
// debugPrint('notification payload: $payload');
// await navigatorKey.currentState!.push(MaterialPageRoute(builder: (context) => SettingsScreen()));
// }
// else if(payload!.length>=6) {
// debugPrint('notification payload: $payload');
// await navigatorKey.currentState!.push(MaterialPageRoute(builder: (context) => Loginpage()));
// }
}
Future setupInteractedMessage() async {
await FirebaseMessaging.instance
.getInitialMessage()
.then((value) => _handleMessage(value != null ? value.data : Map()));
}
void _handleMessage(Map<String, dynamic> data) {
if (data['redirect'] == "Settings") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SettingsScreen(message: data['message'])));
} else if (data['redirect'] == "Login") {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Loginpage(message: data['message'])));
}
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'You will receive notification',
),
],
),
),
);
}
}