I am developing a flutter app with push notification by firebase and i developing in my Iphone. The notification function is working, but i want to add a redirect function(redirect to special page(route name is "/map").
The function onDidReceiveNotificationResponse will be call when i click the notification message, but it will repeat calling the code inside onDidReceiveNotificationResponse but the redirect function is not work.
The log output
flutter: notification payload:
flutter: onDidReceiveNotificationResponse
flutter: notification payload:
flutter: onDidReceiveNotificationResponse
flutter: notification payload:
flutter: onDidReceiveNotificationResponse
class NotificationService {
final notificationPlugin = FlutterLocalNotificationsPlugin();
static final onNotifications = BehaviorSubject<String?>();
bool _isInitialized = false;
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initNotification() async {
print("NotificationService initNotification 1");
if (_isInitialized) return;
const initSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
const initSettingsIOS = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
);
void onDidReceiveNotificationResponse(
NotificationResponse notificationResponse) async {
final String? payload = notificationResponse.payload;
if (notificationResponse.payload != null) {
debugPrint('notification payload: $payload');
debugPrint('onDidReceiveNotificationResponse');
AppGlobal.navigatorKey.currentState?.pushNamed('/map');
}
}
const initSetting = InitializationSettings(
android: initSettingsAndroid, iOS: initSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initSetting,
onDidReceiveNotificationResponse: (notificationResponse) =>
onDidReceiveNotificationResponse(notificationResponse));
_isInitialized = !_isInitialized;
}