Flutter Show a local notification every 3 days

65 Views Asked by At

im programming an app using Flutter and Android Studio. At the moment im trying to make a notify_service, where the user will get a notification local on his phone every 3 days. So you are opening the app the first time, after 3 days you get a notification to open the app again, and after 3 more days you get the exact same notification and so on. Im using Flutter_local_notifications.

Thats my Code so far but I cant get it to work:

notify_service.dart

import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:shared_preferences/shared_preferences.dart';

class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin =
  FlutterLocalNotificationsPlugin();

  // Methode, um die Zeit des letzten Benachrichtigungsversands zu speichern
  Future<void> _setLastNotificationTime() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt('lastNotificationTime', DateTime.now().millisecondsSinceEpoch);
  }

  // Methode, um die Zeit des letzten Benachrichtigungsversands abzurufen
  Future<int?> _getLastNotificationTime() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getInt('lastNotificationTime');
  }

  Future<void> initNotification() async {
    AndroidInitializationSettings initializationSettingsAndroid =
    const AndroidInitializationSettings('@drawable/sn_invent');

    var initializationSettingsIOS = DarwinInitializationSettings(
        requestAlertPermission: true,
        requestBadgePermission: true,
        requestSoundPermission: true,
        onDidReceiveLocalNotification:
            (int id, String? title, String? body, String? payload) async {});

    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    await notificationsPlugin.initialize(initializationSettings,
        onDidReceiveNotificationResponse:
            (NotificationResponse notificationResponse) async {});
  }

  NotificationDetails notificationDetails() {
    return const NotificationDetails(
        android: AndroidNotificationDetails('channelId', 'channelName',
            importance: Importance.max),
        iOS: DarwinNotificationDetails());
  }

  Future<void> showNotification(
      {int id = 0, String? title, String? body, String? payload}) async {
    await notificationsPlugin.show(
        id, title, body, await notificationDetails());

    // Aktualisieren Sie den Zeitstempel für die letzte Benachrichtigung
    await _setLastNotificationTime();
  }

  // Methode, um zu überprüfen, ob seit der letzten Benachrichtigung 3 Tage vergangen sind
  Future<void> checkAndShowNotification() async {
    int? lastNotificationTime = await _getLastNotificationTime();
    int currentTime = DateTime.now().millisecondsSinceEpoch;

    if (lastNotificationTime == null ||
        currentTime - lastNotificationTime >= 3 * 24 * 60 * 60 * 1000) {
      // Mehr als 3 Tage vergangen, also zeige eine neue Benachrichtigung
      await showNotification(
        title: 'Ihre Benachrichtigung',
        body: 'Dies ist Ihre Benachrichtigung nach 3 Tagen.',
      );
    }
  }
}

main.dart

import 'package:flutter/material.dart';
import 'package:flutter_goals_application/src/services/notify_service.dart';
import 'screens/homescreen.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  NotificationService notificationService = NotificationService();
  await notificationService.initNotification();
  runApp(MaterialApp(
    home: HomescreenWidget(),
  ));
}

I hope you guys can help me.

0

There are 0 best solutions below