MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.47k stars 1.4k forks source link

getNotificationAppLaunchDetails always returns null on iOS but not on Android. #2283

Closed rezmeplxrf closed 7 months ago

rezmeplxrf commented 7 months ago

I am testing getNotificationAppLaunchDetails method so that I can navigate to a specific screen when the notification is tapped. It works as intended on Android but not on iOS. on iOS it just returns null. Notification was created using scheduleNotification function.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

class NotificationService {
  static const String _channelId = 'high_importance_channel';
  static const String _channelName = 'High Importance Notifications';
  static const String _channelDescription = 'Notification for SpendWise';

  static Future init() async {
    tz.initializeTimeZones();
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings(
      '@mipmap/ic_launcher',
    );

    final DarwinInitializationSettings initializationSettingsDarwin =
        DarwinInitializationSettings(
            onDidReceiveLocalNotification: (id, title, body, payload) {});

    final InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsDarwin,
    );

    flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
    );

    if (defaultTargetPlatform == TargetPlatform.android) {
      const AndroidNotificationChannel channel = AndroidNotificationChannel(
        _channelId,
        _channelName,
        importance: Importance.max,
      );
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
    }
  }

  static Future showNotification(
      String title, String body, String payload) async {
    AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(_channelId, _channelName,
            channelDescription: _channelDescription,
            importance: Importance.max,
            priority: Priority.high,
            ticker: title);
    const DarwinNotificationDetails darwinNotificationDetails =
        DarwinNotificationDetails(
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
    );

    NotificationDetails notificationDetails = NotificationDetails(
        android: androidNotificationDetails, iOS: darwinNotificationDetails);
    print("payload: $payload");
    print("NotificationDetail: $notificationDetails");
    await flutterLocalNotificationsPlugin
        .show(0, title, body, notificationDetails, payload: payload);
  }

  static Future scheduleNotification(
      String productUrl,
      String title,
      String body,
      String payload,
      DateTime scheduledTime,
      Duration duration) async {
    final tz.TZDateTime tzScheduledTime =
        tz.TZDateTime.from(scheduledTime, tz.local);

    AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails(_channelId, _channelName,
            channelDescription: _channelDescription,
            importance: Importance.max,
            priority: Priority.high,
            ticker: title);
    const DarwinNotificationDetails darwinNotificationDetails =
        DarwinNotificationDetails(
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
    );

    NotificationDetails notificationDetails = NotificationDetails(
        android: androidNotificationDetails, iOS: darwinNotificationDetails);
    await flutterLocalNotificationsPlugin.zonedSchedule(
      productUrl.hashCode,
      title,
      body,
      tzScheduledTime.subtract(duration),
      notificationDetails,
      payload: payload,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
    );
  }

  static void cancelScheduledNotification(String productUrl) async {
    flutterLocalNotificationsPlugin.cancel(productUrl.hashCode);
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await NotificationService.init();
    final NotificationAppLaunchDetails? notificationAppLaunchDetails =
      await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
  print(
      "DEBUG: ${notificationAppLaunchDetails?.didNotificationLaunchApp}");
  print(
      "DEBUG: ${notificationAppLaunchDetails?.notificationResponse?.payload}");

  runApp(ProviderScope(
    observers: [
      MyObserver(),
    ],
    child: AppStartupWidget(
      onLoaded: (context) => const App(),
    ),
  ));
}
Tanejaboy commented 6 months ago

Hey @rezmeplxrf, I'm also getting same issue. Could you please share how did you solved this issue?