-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When launch app from notification tap(when app is terminated):- it is not navigating to specific screen || get initial message not working #16871
Comments
@DeepakYiron, could you please provide a minimal code sample that uses only FlutterFire plugins? This will help us isolate the issue effectively. Additionally, ensure the code is properly formatted for clarity. Thank you! |
------main.dart----- @pragma('vm:entry-point') const AndroidNotificationChannel channel = AndroidNotificationChannel( Future main() async { SystemChrome.setPreferredOrientations([ Future determineInitialRoute() async { return ''; class MyApp extends StatefulWidget { class _MyAppState extends State { late String token; var isBKNotification=false; } -----Notification service class------------- class NotificationService { Future init() async { const AndroidInitializationSettings initializationSettingsAndroid = final DarwinInitializationSettings initializationSettingsIOS = var initializationSettings = InitializationSettings( await flutterLocalNotificationsPlugin.initialize( ); FirebaseMessaging.onMessage.listen( FirebaseMessaging.onMessageOpenedApp.listen( void showLocalNotification(String? title, String? message, [String? payload]) async { ------payload--------- { |
Hi @DeepakYiron, the formatting still isn’t correct, and the code should be as minimal as possible. If you’re having trouble creating a minimal reproducible example, please refer to this guide for assistance. |
NOTE:-This is the separate main file with all code merged at once place: import 'dart:async'; @pragma('vm:entry-point') Future main() async { Future determineInitialRoute() async { class MyApp extends StatefulWidget { class _MyAppState extends State { -----------------------------------------NOTIFICATION SERVICE CLASS---------------------------------- class NotificationService { Future init() async {
} Future onSelectNotificationResponse(NotificationResponse notificationResponse) async { void showLocalNotification(String? title, String? message, [String? payload]) async { ----------------------FIREBASE PACKAGES AND PUSH NOTIFICATION PAYLOAD------------------------------------ i have used these flutter packages:- Notification payload is like below |
Kindly create a sample repository reproducing the issue and share the link here. |
Is there an existing issue for this?
Which plugins are affected?
No response
Which platforms are affected?
No response
Description
Here is my payload
{
"message": {
"token": "my device token",
"data": {
"title": "Custom Notification",
"body": "This is a custom notification with actions.",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"android": {
"priority": "high"
}
}
}
I JUST WANT TO USE CUSTON NOTIFICATION (NOT DEFAULT ONE)
NOTE:- in default notification everything is working fine nut what if i need to use my own custom notification with action etc
when i change payload from this to give below its working fine when app in launched from notification when app is terminated
i am getting issue only when app is terminated and launched from notification tap
{
"message": {
"token": "my device token",
"notification": {
"title": "Custom Notification",
"body": "This is a custom notification with actions.",
},
"android": {
"priority": "high"
}
}
}
i am using data:{} instead of notification:{} because notification trigger default push notification:- i am using my custom notification and working fine when app is foreground and background but not in case when app is terminated and launched from notification tap
please suggest:-
FirebaseMessaging messaging = FirebaseMessaging.instance;
RemoteMessage? initialMessage = await messaging.getInitialMessage();
this method is not working when i used data:{} in payload
Reproducing the issue
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import "package:flutter/material.dart";
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:notification_permissions/notification_permissions.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:t_three_crm/constants/constants.dart';
import 'package:t_three_crm/push_notification/notification_service.dart';
import 'package:t_three_crm/resources/font_family.dart';
import 'package:t_three_crm/screens/login/view/LoginScreen.dart';
import 'package:t_three_crm/screens/login/view/reset_password_screen.dart';
import 'dart:io';
import 'package:t_three_crm/screens/main_dashboard/view/main_dashboard.dart';
@pragma('vm:entry-point')
Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
NotificationService().showLocalNotification(
message.data['title']??"",
message.data['body']??"",
"",
);
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
importance: Importance.high,
);
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
//initialize background
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
HttpOverrides.global = MyHttpOverrides();
NotificationService().init();
String initialRoute = await determineInitialRoute();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]).then((_) {
runApp(MyApp(initialRoute: initialRoute,));
});
}
Future determineInitialRoute() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
RemoteMessage? initialMessage = await messaging.getInitialMessage();
if (initialMessage != null) {
return "Profile";
}
return '';
}
class MyApp extends StatefulWidget {
MyApp({Key? key, required this.initialRoute}) : super(key: key);
final String initialRoute;
@OverRide
State createState() => _MyAppState();
}
class _MyAppState extends State {
late String token;
getToken() async {
token = (await FirebaseMessaging.instance.getToken())!;
Constants.fcmToken=token;
print(token);
}
var isBKNotification=false;
@OverRide
void initState() {
super.initState();
getToken();
}
@OverRide
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: AppFontFamily.gothic,
useMaterial3: false,
appBarTheme: AppBarTheme(
surfaceTintColor: Colors.transparent,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.transparent
)
),
datePickerTheme: DatePickerThemeData(
headerForegroundColor: Colors.white,
backgroundColor: Colors.white,
headerBackgroundColor: Colors.blue,
dividerColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0), // this is the border radius of the picker
),
),
scaffoldBackgroundColor: Colors.white,
// colorScheme: ColorScheme.fromSeed(onPrimary: Colors.white, seedColor: Colors.white)
),
//for notification tap navigate
home:widget.initialRoute=="Profile"?ProfileScreen(): Splash(),
}
}
import 'dart:convert';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:t_three_crm/screens/home/view/dashboard.dart';
class NotificationService {
final _messaging = FirebaseMessaging.instance;
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future init() async {
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission();
}
Future onSelectNotificationResponse(
NotificationResponse notificationResponse) async {
final String? payload = notificationResponse.payload;
if (payload != null) {
await Get.to(DashBoard(index: 0));
/// this will execute and navigate to screen when tap on notification when app is in foreground and even in background and payload for this is
/// this will work in both scenario (BACKGROUND & FOREGROUND)
}
void showLocalNotification(String? title, String? message, [String? payload]) async {
AndroidNotificationDetails androidPlatformChannelSpecifics = AndroidNotificationDetails(
title ?? '',
message ?? '',
importance: Importance.max,
priority: Priority.high,
enableVibration: true, // Enable vibration
playSound: true,
showWhen: false,
icon: "@drawable/ic_launcher"
);
DarwinNotificationDetails iosPlatformChannelSpecifics =
DarwinNotificationDetails(presentBadge: true, presentSound: true, presentAlert: true,);
NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iosPlatformChannelSpecifics,
);
await flutterLocalNotificationsPlugin.show(
0,
title,
message,
platformChannelSpecifics,
payload: payload,
);
}
}
Firebase Core version
3.1.0
Flutter Version
3.22.2
Relevant Log Output
No response
Flutter dependencies
Expand
Flutter dependencies
snippetReplace this line with the contents of your `flutter pub deps -- --style=compact`.
Additional context and comments
No response
The text was updated successfully, but these errors were encountered: